package com.xscm.moduleutil.widget; import android.content.Context; import android.util.DisplayMetrics; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import androidx.annotation.Nullable; import com.xscm.moduleutil.R; import com.xscm.moduleutil.bean.UserOnlineStatusBean; import com.xscm.moduleutil.bean.room.RoomPitBean; import java.util.List; public class WheatLayoutManager { private final Context context; private final ViewGroup container; private List pitList; private boolean isSingleMode = false; private int currentSinglePit = -1; private RoomDefaultWheatView singleWheatView; // 麦位索引映射:9,10,1~8 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 pitList) { if (pitList == null || pitList.size() < 10) return; this.pitList = pitList; restoreMultiWheat(); } public void setWheatDataPk(List pitList, int layoutType) { if (pitList == null || pitList.size() < 10) return; 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; if (pitNumber < 1 || pitNumber > 10 || pitList == null || pitList.size() < 10) return; container.removeAllViews(); RoomPitBean bean = pitList.get(pitNumber - 1); singleWheatView = new RoomDefaultWheatView(context); singleWheatView.pitNumber = String.valueOf(pitNumber); singleWheatView.setData(bean); 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; LinearLayout row = new LinearLayout(context); row.setOrientation(LinearLayout.HORIZONTAL); for (int i = 0; i < 10; i++) { int pitNumber = pitIndexMap[i]; RoomDefaultWheatView wheatView = createWheatView(pitNumber); LinearLayout.LayoutParams params; if (i == 0 || i == 1) { int fixedHeightInPx = dpToPx(110); params = new LinearLayout.LayoutParams(itemWidth, fixedHeightInPx); if (i == 0) params.rightMargin = dpToPx(50); } else { int fixedHeightInPx = dpToPx(90); params = new LinearLayout.LayoutParams(itemWidth - 30, fixedHeightInPx + 30); params.setMargins(0, 0, 0, 0); } wheatView.setLayoutParams(params); wheatView.setOnClickListener(v -> { if (wheatClickListener != null) { wheatClickListener.onWheatClick(wheatView, Integer.parseInt(wheatView.pitNumber)); } }); row.addView(wheatView); if (i == 1 || (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; } public void restoreMultiWheatPk(int layoutType) { container.removeAllViews(); int screenWidth = getScreenWidth(); int itemWidth = screenWidth / 8; LinearLayout row = new LinearLayout(context); row.setOrientation(LinearLayout.HORIZONTAL); int firstPitNumber = layoutType == 1 ? 10 : 9; int secondPitNumber = layoutType == 1 ? 9 : 10; addWheatViewItem(row, firstPitNumber, itemWidth * 2, layoutType); addWheatViewItem(row, secondPitNumber, itemWidth * 2, layoutType); container.addView(row); row = new LinearLayout(context); row.setOrientation(LinearLayout.HORIZONTAL); for (int i = 2; i < 10; i++) { int pitNumber = pitIndexMap[i]; addWheatViewItem(row, pitNumber, itemWidth, layoutType); if ((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 = createWheatView(pitNumber); 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) { params.rightMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_1); params.setMargins(20, -30, -20, 0); } else { 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) { params.setMargins(-30, 10, 0, 0); } else { 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 int dpToPx(int dp) { return Math.round(dp * context.getResources().getDisplayMetrics().density); } private int getScreenWidth() { DisplayMetrics metrics = context.getResources().getDisplayMetrics(); return metrics.widthPixels; } 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) { wheatView.setData(pitBean); } } @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 newPitList, List changedPits) { this.pitList = newPitList; for (int pitNumber : changedPits) { updateSingleWheat(pitList.get(pitNumber - 1), pitNumber); } } public void updateSingleOnlineWheat(UserOnlineStatusBean bean) { if (pitList == null || pitList.isEmpty() || bean == null) return; for (RoomPitBean pitBean : pitList) { int pitNumber = Integer.parseInt(pitBean.getPit_number()); RoomDefaultWheatView wheatView = findWheatViewByPitNumber(pitNumber); if (wheatView != null) { wheatView.setOnlineStatus(bean); } } } public void clear() { container.removeAllViews(); pitList = null; singleWheatView = null; isSingleMode = false; currentSinglePit = -1; } public void clearData() { pitList = null; singleWheatView = null; isSingleMode = false; currentSinglePit = -1; } }