429 lines
16 KiB
Java
429 lines
16 KiB
Java
package com.qxcm.moduleutil.widget;
|
||
|
||
import android.content.Context;
|
||
import android.util.DisplayMetrics;
|
||
import android.view.Gravity;
|
||
import android.view.View;
|
||
import android.view.ViewGroup;
|
||
import android.widget.ImageView;
|
||
import android.widget.LinearLayout;
|
||
|
||
import androidx.annotation.Nullable;
|
||
import androidx.constraintlayout.widget.ConstraintLayout;
|
||
import androidx.recyclerview.widget.GridLayoutManager;
|
||
import androidx.recyclerview.widget.RecyclerView;
|
||
|
||
import com.google.android.flexbox.FlexboxLayout;
|
||
import com.qxcm.moduleutil.R;
|
||
import com.qxcm.moduleutil.bean.room.RoomPitBean;
|
||
|
||
import java.util.List;
|
||
|
||
public class WheatLayoutManager {
|
||
private final Context context;
|
||
private final ViewGroup container;
|
||
private List<RoomPitBean> pitList;
|
||
private boolean isSingleMode = false;
|
||
private int currentSinglePit = -1;
|
||
private RoomDefaultWheatView singleWheatView;
|
||
|
||
private final int[] pitIndexMap = {9, 10, 1, 2, 3, 4, 5, 6, 7, 8};
|
||
|
||
public interface OnWheatClickListener {
|
||
void onWheatClick(RoomDefaultWheatView view, int pitNumber);
|
||
|
||
void onMakeWheatClick(RoomDefaultWheatView view, int pitNumber);
|
||
|
||
}
|
||
|
||
private @Nullable OnWheatClickListener wheatClickListener;
|
||
|
||
public WheatLayoutManager(Context context, ViewGroup container) {
|
||
this.context = context;
|
||
this.container = container;
|
||
}
|
||
|
||
public void setWheatData(List<RoomPitBean> pitList) {
|
||
this.pitList = pitList;
|
||
restoreMultiWheat();
|
||
}
|
||
|
||
public void setWheatDataPk(List<RoomPitBean> pitList, int layoutType) {
|
||
this.pitList = pitList;
|
||
restoreMultiWheatPk(layoutType);
|
||
}
|
||
|
||
public void setOnWheatClickListener(@Nullable OnWheatClickListener listener) {
|
||
this.wheatClickListener = listener;
|
||
}
|
||
|
||
public void showSingleWheat(int pitNumber) {
|
||
if (isSingleMode && this.currentSinglePit == pitNumber) return;
|
||
|
||
container.removeAllViews();
|
||
|
||
if (pitNumber < 1 || pitNumber > 10 || pitList == null || pitList.size() < 10)
|
||
return;
|
||
|
||
RoomPitBean bean = pitList.get(pitNumber - 1);
|
||
|
||
singleWheatView = new RoomDefaultWheatView(context);
|
||
singleWheatView.pitNumber = String.valueOf(pitNumber);
|
||
singleWheatView.setData(bean);
|
||
|
||
// 默认设置为 MATCH_PARENT,也可以自定义
|
||
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
|
||
LinearLayout.LayoutParams.MATCH_PARENT,
|
||
LinearLayout.LayoutParams.WRAP_CONTENT
|
||
);
|
||
params.setMargins(20, 20, 20, 20);
|
||
singleWheatView.setLayoutParams(params);
|
||
|
||
// 添加点击事件
|
||
singleWheatView.setOnClickListener(v -> {
|
||
if (wheatClickListener != null) {
|
||
wheatClickListener.onWheatClick(singleWheatView, pitNumber);
|
||
}
|
||
restoreMultiWheat();
|
||
});
|
||
|
||
container.addView(singleWheatView);
|
||
isSingleMode = true;
|
||
currentSinglePit = pitNumber;
|
||
}
|
||
|
||
public void restoreMultiWheat() {
|
||
container.removeAllViews();
|
||
|
||
int screenWidth = getScreenWidth();
|
||
int itemWidth = screenWidth / 4; // 每个控件宽度为屏幕宽度的 1/4
|
||
|
||
LinearLayout row = new LinearLayout(context);
|
||
row.setOrientation(LinearLayout.HORIZONTAL);
|
||
|
||
for (int i = 0; i < 10; i++) {
|
||
int pitNumber = pitIndexMap[i];
|
||
RoomDefaultWheatView wheatView = new RoomDefaultWheatView(context);
|
||
wheatView.pitNumber = String.valueOf(pitNumber);
|
||
wheatView.setData(pitList.get(pitNumber - 1));
|
||
|
||
LinearLayout.LayoutParams params;
|
||
|
||
if (i == 0) {
|
||
int fixedHeightInDp = 110; // 固定高度为 100dp
|
||
int fixedHeightInPx = dpToPx(fixedHeightInDp); // 调用已有的 dpToPx 方法
|
||
// 第一个控件:左边距 86dp,右边距 100dp
|
||
params = new LinearLayout.LayoutParams(itemWidth, fixedHeightInPx);
|
||
params.rightMargin = dpToPx(50);
|
||
} else if (i == 1) {
|
||
int fixedHeightInDp = 110; // 固定高度为 100dp
|
||
int fixedHeightInPx = dpToPx(fixedHeightInDp); // 调用已有的 dpToPx 方法
|
||
// 第二个控件:右边距 86dp
|
||
params = new LinearLayout.LayoutParams(itemWidth, fixedHeightInPx);
|
||
} else {
|
||
int fixedHeightInDp = 90; // 固定高度为 100dp
|
||
int fixedHeightInPx = dpToPx(fixedHeightInDp); // 调用已有的 dpToPx 方法
|
||
params = new LinearLayout.LayoutParams(itemWidth - 30, fixedHeightInPx + 30);
|
||
// 其他控件保持原有逻辑
|
||
|
||
|
||
// if (i > 1 && (i - 2) % 4 != 0) {
|
||
// params.leftMargin = 12;
|
||
// params.rightMargin = 12;
|
||
// }
|
||
params.setMargins(0, 0, 0, 0); // 不设右边距,由 row padding 控制
|
||
}
|
||
|
||
wheatView.setLayoutParams(params);
|
||
wheatView.setOnClickListener(v -> {
|
||
if (wheatClickListener != null) {
|
||
wheatClickListener.onWheatClick(wheatView, Integer.parseInt(wheatView.pitNumber));
|
||
}
|
||
// showSingleWheat(Integer.parseInt(wheatView.pitNumber));
|
||
});
|
||
|
||
row.addView(wheatView);
|
||
|
||
// 第一行添加两个后换行
|
||
if (i == 1) {
|
||
container.addView(row);
|
||
row = new LinearLayout(context);
|
||
row.setOrientation(LinearLayout.HORIZONTAL);
|
||
} else if (i > 1 && (i - 2) % 4 == 3) {
|
||
container.addView(row);
|
||
row = new LinearLayout(context);
|
||
row.setOrientation(LinearLayout.HORIZONTAL);
|
||
}
|
||
}
|
||
|
||
// 添加最后一行可能存在的剩余 view
|
||
if (row.getChildCount() > 0) {
|
||
container.addView(row);
|
||
}
|
||
isSingleMode = false;
|
||
currentSinglePit = -1;
|
||
|
||
}
|
||
// public void restoreMultiWheatPk(int layoutType, int width) {
|
||
// container.removeAllViews();
|
||
//
|
||
// int screenWidth = getScreenWidth();
|
||
// int itemWidth = screenWidth / 8; // 每个控件宽度为屏幕宽度的 1/4
|
||
//
|
||
// LinearLayout row = new LinearLayout(context);
|
||
// row.setOrientation(LinearLayout.HORIZONTAL);
|
||
//
|
||
// for (int i = 0; i < 10; i++) {
|
||
// int pitNumber = pitIndexMap[i];
|
||
// RoomDefaultWheatView wheatView = new RoomDefaultWheatView(context);
|
||
// wheatView.pitNumber = String.valueOf(pitNumber);
|
||
// wheatView.setData(pitList.get(pitNumber - 1));
|
||
//
|
||
// LinearLayout.LayoutParams params;
|
||
//
|
||
// if (i == 0) {
|
||
// int fixedHeightInDp = 110; // 固定高度为 100dp
|
||
// int fixedHeightInPx =context.getResources().getDimensionPixelSize(R.dimen.dp_80); // 调用已有的 dpToPx 方法
|
||
// // 第一个控件:左边距 86dp,右边距 100dp
|
||
// params = new LinearLayout.LayoutParams(itemWidth, fixedHeightInPx);
|
||
// params.rightMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_50);
|
||
// } else if (i == 1) {
|
||
// int fixedHeightInDp = 110; // 固定高度为 100dp
|
||
// int fixedHeightInPx = context.getResources().getDimensionPixelSize(R.dimen.dp_80); // 调用已有的 dpToPx 方法
|
||
// // 第二个控件:右边距 86dp
|
||
// params = new LinearLayout.LayoutParams(itemWidth, fixedHeightInPx);
|
||
// } else {
|
||
// int fixedHeightInDp = 90; // 固定高度为 100dp
|
||
// int fixedHeightInPx = context.getResources().getDimensionPixelSize(R.dimen.dp_60); // 调用已有的 dpToPx 方法
|
||
// params = new LinearLayout.LayoutParams(itemWidth -10, fixedHeightInPx + 30);
|
||
// // 其他控件保持原有逻辑
|
||
//
|
||
//
|
||
//// if (i > 1 && (i - 2) % 4 != 0) {
|
||
//// params.leftMargin = 12;
|
||
//// params.rightMargin = 12;
|
||
//// }
|
||
// params.setMargins(0, 0, 0, 0); // 不设右边距,由 row padding 控制
|
||
// }
|
||
//
|
||
// wheatView.setLayoutParams(params);
|
||
// wheatView.setOnClickListener(v -> {
|
||
// if (wheatClickListener != null) {
|
||
// wheatClickListener.onWheatClick(wheatView, Integer.parseInt(wheatView.pitNumber));
|
||
// }
|
||
//// showSingleWheat(Integer.parseInt(wheatView.pitNumber));
|
||
// });
|
||
//
|
||
// row.addView(wheatView);
|
||
//
|
||
// // 第一行添加两个后换行
|
||
// if (i == 1) {
|
||
// container.addView(row);
|
||
// row = new LinearLayout(context);
|
||
// row.setOrientation(LinearLayout.HORIZONTAL);
|
||
// } else if (i > 1 && (i - 2) % 4 == 3) {
|
||
// container.addView(row);
|
||
// row = new LinearLayout(context);
|
||
// row.setOrientation(LinearLayout.HORIZONTAL);
|
||
// }
|
||
// }
|
||
//
|
||
//// 添加最后一行可能存在的剩余 view
|
||
// if (row.getChildCount() > 0) {
|
||
// container.addView(row);
|
||
// }
|
||
// isSingleMode = false;
|
||
// currentSinglePit = -1;
|
||
// }
|
||
|
||
|
||
public void restoreMultiWheatPk(int layoutType) {
|
||
if (layoutType == 1) {
|
||
container.removeAllViews();
|
||
}
|
||
int screenWidth = getScreenWidth();
|
||
int itemWidth = screenWidth / 8;
|
||
|
||
LinearLayout row = new LinearLayout(context);
|
||
row.setOrientation(LinearLayout.HORIZONTAL);
|
||
|
||
// 根据 layoutType 调整前两个控件的顺序
|
||
int firstPitNumber, secondPitNumber;
|
||
if (layoutType == 1) {
|
||
firstPitNumber = 10; // 第一个显示 10
|
||
secondPitNumber = 9; // 第二个显示 9
|
||
} else if (layoutType == 2) {
|
||
firstPitNumber = 9; // 第一个显示 9
|
||
secondPitNumber = 10; // 第二个显示 10
|
||
} else {
|
||
firstPitNumber = 9;
|
||
secondPitNumber = 10;
|
||
}
|
||
|
||
// 添加第一个控件(10 或 9)
|
||
addWheatViewItem(row, firstPitNumber, itemWidth*2, layoutType);
|
||
|
||
// 添加第二个控件(9 或 10)
|
||
addWheatViewItem(row, secondPitNumber, itemWidth*2, layoutType);
|
||
|
||
container.addView(row);
|
||
row = new LinearLayout(context);
|
||
row.setOrientation(LinearLayout.HORIZONTAL);
|
||
|
||
// 添加其余 8 个控件(1~8)
|
||
for (int i = 2; i < 10; i++) {
|
||
int pitNumber = pitIndexMap[i];
|
||
addWheatViewItem(row, pitNumber, itemWidth, layoutType);
|
||
|
||
if (i > 1 && (i - 2) % 4 == 3) {
|
||
container.addView(row);
|
||
row = new LinearLayout(context);
|
||
row.setOrientation(LinearLayout.HORIZONTAL);
|
||
}
|
||
}
|
||
|
||
if (row.getChildCount() > 0) {
|
||
container.addView(row);
|
||
}
|
||
|
||
isSingleMode = false;
|
||
currentSinglePit = -1;
|
||
}
|
||
|
||
// 抽取公共方法:添加单个控件
|
||
private void addWheatViewItem(LinearLayout row, int pitNumber, int itemWidth, int layoutType) {
|
||
RoomDefaultWheatView wheatView = new RoomDefaultWheatView(context);
|
||
wheatView.pitNumber = String.valueOf(pitNumber);
|
||
wheatView.setData(pitList.get(pitNumber - 1));
|
||
|
||
LinearLayout.LayoutParams params;
|
||
|
||
if (pitNumber == 9 || pitNumber == 10) {
|
||
int fixedHeightInPx = context.getResources().getDimensionPixelSize(R.dimen.dp_90);
|
||
|
||
if (pitNumber == 9) {
|
||
params = new LinearLayout.LayoutParams(itemWidth-40, fixedHeightInPx);
|
||
if (layoutType == 1) {
|
||
// 9号在右边,右边距10dp
|
||
params.rightMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_1);
|
||
params.setMargins(20, -30, -20, 0);
|
||
} else if (layoutType == 2) {
|
||
// 9号在左边,左边距10dp
|
||
params.leftMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_1);
|
||
params.setMargins(-30, -20, 0, 0);
|
||
}
|
||
|
||
} else {
|
||
params = new LinearLayout.LayoutParams(itemWidth - 80, fixedHeightInPx);
|
||
if (layoutType == 1) {
|
||
// 10号在左边,左边距15dp
|
||
// params.leftMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_5);
|
||
params.setMargins(-30, 10, 0, 0);
|
||
} else if (layoutType == 2) {
|
||
// 10号在右边,右边距15dp
|
||
// params.rightMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_5);
|
||
params.setMargins(0, 10, -30, 0);
|
||
}
|
||
|
||
}
|
||
|
||
} else {
|
||
int fixedHeightInPx = context.getResources().getDimensionPixelSize(R.dimen.dp_60);
|
||
params = new LinearLayout.LayoutParams(itemWidth + 15, fixedHeightInPx + 20);
|
||
params.setMargins(-20, -20, -20, 0);
|
||
}
|
||
|
||
wheatView.setLayoutParams(params);
|
||
wheatView.setOnClickListener(v -> {
|
||
if (wheatClickListener != null) {
|
||
if (layoutType==1) {
|
||
wheatClickListener.onWheatClick(wheatView, Integer.parseInt(wheatView.pitNumber));
|
||
}else {
|
||
wheatClickListener.onMakeWheatClick(wheatView, Integer.parseInt(wheatView.pitNumber));
|
||
}
|
||
}
|
||
});
|
||
|
||
row.addView(wheatView);
|
||
}
|
||
|
||
|
||
private RoomDefaultWheatView createWheatView(int pitNumber) {
|
||
RoomDefaultWheatView wheatView = new RoomDefaultWheatView(context);
|
||
wheatView.pitNumber = String.valueOf(pitNumber);
|
||
wheatView.setData(pitList.get(pitNumber - 1));
|
||
return wheatView;
|
||
}
|
||
|
||
private RoomMakeWheatView createRoomMakeWheatView(int pitNumber) {
|
||
RoomMakeWheatView wheatView = new RoomMakeWheatView(context);
|
||
wheatView.pitNumber = String.valueOf(pitNumber);
|
||
wheatView.setData(pitList.get(pitNumber - 1));
|
||
return wheatView;
|
||
}
|
||
|
||
|
||
private int dpToPx(int dp) {
|
||
return Math.round(dp * context.getResources().getDisplayMetrics().density);
|
||
}
|
||
|
||
private int getScreenWidth() {
|
||
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
|
||
return metrics.widthPixels;
|
||
}
|
||
|
||
/**
|
||
* 更新指定 pitNumber 的麦位信息(用于局部刷新)
|
||
*/
|
||
public void updateSingleWheat(RoomPitBean pitBean, int pitNumber) {
|
||
if (pitList == null || pitList.isEmpty() || pitNumber < 1 || pitNumber > 10) return;
|
||
|
||
// 如果是单个展示模式且不是当前麦位,不处理
|
||
if (isSingleMode && this.currentSinglePit != pitNumber) return;
|
||
|
||
RoomDefaultWheatView wheatView = findWheatViewByPitNumber(pitNumber);
|
||
if (wheatView != null) {
|
||
|
||
// RoomPitBean bean = pitList.get(pitNumber - 1);
|
||
RoomPitBean bean = pitBean;
|
||
wheatView.setData(bean); // 刷新数据
|
||
}
|
||
}
|
||
|
||
@Nullable
|
||
private RoomDefaultWheatView findWheatViewByPitNumber(int pitNumber) {
|
||
for (int i = 0; i < container.getChildCount(); i++) {
|
||
View row = container.getChildAt(i);
|
||
if (row instanceof LinearLayout) {
|
||
LinearLayout linearRow = (LinearLayout) row;
|
||
for (int j = 0; j < linearRow.getChildCount(); j++) {
|
||
View child = linearRow.getChildAt(j);
|
||
if (child instanceof RoomDefaultWheatView) {
|
||
RoomDefaultWheatView view = (RoomDefaultWheatView) child;
|
||
if (Integer.parseInt(view.pitNumber) == pitNumber) {
|
||
return view;
|
||
}
|
||
}
|
||
}
|
||
} else if (row instanceof RoomDefaultWheatView) {
|
||
RoomDefaultWheatView view = (RoomDefaultWheatView) row;
|
||
if (Integer.parseInt(view.pitNumber) == pitNumber) {
|
||
return view;
|
||
}
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 批量刷新多个麦位状态
|
||
*/
|
||
public void refreshWheatData(List<RoomPitBean> newPitList, List<Integer> changedPits) {
|
||
this.pitList = newPitList;
|
||
for (int pitNumber : changedPits) {
|
||
// updateSingleWheat(pitNumber);
|
||
}
|
||
}
|
||
|
||
}
|