Files
yusheng-android/BaseModule/src/main/java/com/xscm/moduleutil/widget/WheatLayoutManager.java
2025-11-07 09:22:39 +08:00

354 lines
13 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.RoomMessageEvent;
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<RoomPitBean> 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<RoomPitBean> pitList) {
if (pitList == null || pitList.size() < 10) return;
this.pitList = pitList;
restoreMultiWheat();
}
public void setWheatDataPk(List<RoomPitBean> 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, (int)(itemWidth * 1.7), layoutType);
addWheatViewItem(row, secondPitNumber,(int)(itemWidth * 1.7), 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);
if (layoutType==2){
wheatView.setTv_time_pk(false);
}
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 , fixedHeightInPx);
if (layoutType == 1) {
params.rightMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_1);
params.setMargins(20, -20, -20, 0);
} else {
params.leftMargin = context.getResources().getDimensionPixelSize(R.dimen.dp_1);
params.setMargins(-30, -20, 0, 0);
}
} else {
params = new LinearLayout.LayoutParams(itemWidth , fixedHeightInPx);
if (layoutType == 1) {
params.setMargins(-10, 10, 0, 0);
} else {
params.setMargins(0, 10, -10, 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 setUpData(RoomMessageEvent event,int layoutType){
String fromPit=event.getText().getFrom_pit_number()!=null?event.getText().getFrom_pit_number():"";
String toPitNumber=event.getText().getTo_pit_number()!=null?event.getText().getTo_pit_number():"";
if (fromPit.equals("") || toPitNumber.equals("")){
return;
}
RoomDefaultWheatView fromWheatView = findWheatViewByPitNumber(Integer.parseInt(fromPit));
RoomDefaultWheatView toWheatView = findWheatViewByPitNumber(Integer.parseInt(toPitNumber));
RoomPitBean fromPitBean=fromWheatView.pitBean;
RoomPitBean toPitBean=toWheatView.pitBean;
String tmpNumber=fromPitBean.getPit_number();
fromPitBean.setPit_number(toPitBean.getPit_number());
toPitBean.setPit_number(tmpNumber);
toWheatView.setData(fromPitBean);
fromWheatView.setData(toPitBean);
if (layoutType==2){
fromWheatView.setTv_time_pk(false);
toWheatView.setTv_time_pk(false);
}else if (layoutType==1){
fromWheatView.setTv_time_pk(true);
toWheatView.setTv_time_pk(true);
}
}
public void updateSingleWheat(RoomPitBean pitBean, int pitNumber,int layoutType) {
if (pitList == null || pitList.isEmpty() || pitNumber < 1 || pitNumber > 10) return;
if (isSingleMode && this.currentSinglePit != pitNumber) return;
RoomDefaultWheatView wheatView = findWheatViewByPitNumber(pitNumber);
if (wheatView != null) {
pitBean.set_pk(true);
wheatView.setData(pitBean);
if (layoutType==2){
wheatView.setTv_time_pk(false);
}else if (layoutType==1){
wheatView.setTv_time_pk(true);
}
}
}
public void updateSingleCharm(RoomPitBean pitBean, int pitNumber,int layoutType) {
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=wheatView.pitBean;
bean=pitBean ;
if (layoutType==2){
wheatView.setTv_time_pk(false);
}else if (layoutType==1){
wheatView.setTv_time_pk(true);
}
wheatView.setSex(bean.getCharm(),false);
}
}
@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(pitList.get(pitNumber - 1), pitNumber,1);
}
}
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();
}
}
}
public void clear() {
container.removeAllViews();
pitList = null;
singleWheatView = null;
isSingleMode = false;
currentSinglePit = -1;
}
public void clearData() {
pitList = null;
singleWheatView = null;
isSingleMode = false;
currentSinglePit = -1;
}
}