498 lines
19 KiB
Java
498 lines
19 KiB
Java
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.google.android.material.snackbar.Snackbar;
|
||
import com.hjq.toast.ToastUtils;
|
||
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.ArrayList;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* @Author lxj$
|
||
* @Time 2025-8-6 17:29:04$ $
|
||
* @Description 二卡八显示布局$
|
||
*/
|
||
public class WheatLayoutSingManager {
|
||
private final Context context;
|
||
private final ViewGroup container;
|
||
private List<RoomPitBean> pitList;
|
||
private boolean isSingleMode = false;
|
||
private int currentSinglePit = -1;
|
||
private RoomSingSongWheatView singleWheatView;
|
||
private List<RoomSingSongWheatView> multiWheatViews=new ArrayList<>();
|
||
|
||
private final int[] pitIndexMap = {9, 10, 1, 2, 3, 4, 5, 6, 7, 8};
|
||
|
||
public interface OnWheatClickListener {
|
||
void onWheatClick(RoomSingSongWheatView view, int pitNumber);
|
||
|
||
void onMakeWheatClick(RoomSingSongWheatView view, int pitNumber);
|
||
|
||
void onMeilingClick(RoomSingSongWheatView view, int pitNumber);
|
||
|
||
}
|
||
|
||
private @Nullable OnWheatClickListener wheatClickListener;
|
||
|
||
public WheatLayoutSingManager(Context context, ViewGroup container) {
|
||
this.context = context;
|
||
this.container = container;
|
||
}
|
||
|
||
public void setWheatData(List<RoomPitBean> pitList, boolean isFirst) {
|
||
// 检查容器状态
|
||
if (container == null) {
|
||
return;
|
||
}
|
||
|
||
this.pitList = pitList;
|
||
restoreMultiWheat(isFirst);
|
||
}
|
||
|
||
public void setOnWheatClickListener(@Nullable OnWheatClickListener listener) {
|
||
this.wheatClickListener = listener;
|
||
}
|
||
|
||
public void restoreMultiWheat(boolean isFirst) {
|
||
// 检查容器状态
|
||
if (container == null) {
|
||
return;
|
||
}
|
||
|
||
try {
|
||
if (multiWheatViews != null && multiWheatViews.size() != 10) {
|
||
container.removeAllViews();
|
||
}
|
||
} catch (Exception e) {
|
||
// 忽略异常,继续执行
|
||
return;
|
||
}
|
||
|
||
if (pitList == null || pitList.size() < 10) {
|
||
return;
|
||
}
|
||
|
||
// multiWheatViews.clear();
|
||
|
||
int screenWidth = getScreenWidth();
|
||
int itemWidth = screenWidth / 4; // 每个控件宽度为屏幕宽度的 1/4
|
||
|
||
LinearLayout row = new LinearLayout(context);
|
||
row.setOrientation(LinearLayout.HORIZONTAL);
|
||
if (multiWheatViews.size() == 10){
|
||
for (int i = 0; i < multiWheatViews.size(); i++){
|
||
multiWheatViews.get(i).setData(pitList.get(pitIndexMap[i] - 1));
|
||
}
|
||
}else {
|
||
for (int i = 0; i < 10; i++) {
|
||
int pitNumber = pitIndexMap[i];
|
||
if (isFirst){
|
||
RoomSingSongWheat2View wheatView = new RoomSingSongWheat2View( 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);
|
||
// 其他控件保持原有逻辑
|
||
|
||
params.setMargins(0, 0, 0, 0); // 不设右边距,由 row padding 控制
|
||
}
|
||
wheatView.setLayoutParams(params);
|
||
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);
|
||
}
|
||
|
||
}else {
|
||
RoomSingSongWheatView wheatView = new RoomSingSongWheatView(context);
|
||
wheatView.pitNumber = String.valueOf(pitNumber);
|
||
wheatView.setData(pitList.get(pitNumber - 1));
|
||
multiWheatViews.add(wheatView);
|
||
|
||
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);
|
||
// 其他控件保持原有逻辑
|
||
|
||
params.setMargins(0, 0, 0, 0); // 不设右边距,由 row padding 控制
|
||
}
|
||
wheatView.setLayoutParams(params);
|
||
|
||
|
||
GifAvatarOvalView avatarView = (GifAvatarOvalView) wheatView.mRiv;
|
||
avatarView.setOnClickListener(v -> {
|
||
if (wheatClickListener != null) {
|
||
wheatClickListener.onWheatClick(wheatView, pitNumber);
|
||
}
|
||
});
|
||
|
||
WheatCharmView charmView = wheatView.mCharmView;
|
||
charmView.setOnClickListener(v -> {
|
||
if (wheatClickListener != null) {
|
||
ToastUtils.show("点击了麦位");
|
||
wheatClickListener.onMeilingClick(wheatView, pitNumber);
|
||
}
|
||
});
|
||
|
||
wheatView.setOnClickListener(v -> {
|
||
if (wheatClickListener != null) {
|
||
wheatClickListener.onMeilingClick(wheatView, 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 (container == null || !isContainerValid()) {
|
||
return;
|
||
}
|
||
|
||
if (layoutType == 1) {
|
||
try {
|
||
container.removeAllViews();
|
||
} catch (Exception e) {
|
||
// 忽略异常,继续执行
|
||
return;
|
||
}
|
||
}
|
||
|
||
if (pitList == null || pitList.size() < 10) {
|
||
return;
|
||
}
|
||
|
||
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) {
|
||
// 检查容器状态
|
||
if (!isContainerValid()) {
|
||
return;
|
||
}
|
||
|
||
RoomSingSongWheatView wheatView = new RoomSingSongWheatView(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.setMargins(-30, 10, 0, 0);
|
||
} else if (layoutType == 2) {
|
||
// 10号在右边,右边距15dp
|
||
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 RoomSingSongWheatView createWheatView(int pitNumber) {
|
||
RoomSingSongWheatView wheatView = new RoomSingSongWheatView(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;
|
||
}
|
||
|
||
public void setUpData(RoomMessageEvent event){
|
||
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;
|
||
}
|
||
RoomSingSongWheatView fromWheatView = findWheatViewByPitNumber(Integer.parseInt(fromPit));
|
||
RoomSingSongWheatView 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);
|
||
for (RoomSingSongWheatView view : multiWheatViews){
|
||
if (view.pitBean.getUser_id().equals(event.getText().getFromUserInfo().getUser_id()+"")){
|
||
if (!view.pitBean.getPit_number().equals(toPitNumber)){
|
||
RoomPitBean bean=view.pitBean;
|
||
bean.setCharm("");
|
||
bean.setUser_id("");
|
||
bean.setDress("");
|
||
bean.setAvatar("");
|
||
bean.setNickname("");
|
||
bean.setSex("");
|
||
bean.setUser_code("");
|
||
bean.setDress_picture("");
|
||
view.setData(bean);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 更新指定 pitNumber 的麦位信息(用于局部刷新)
|
||
*/
|
||
public void updateSingleWheat(RoomPitBean pitBean, int pitNumber) {
|
||
// 检查容器状态
|
||
if (container == null || !isContainerValid()) {
|
||
return;
|
||
}
|
||
|
||
if (pitList == null || pitList.isEmpty() || pitNumber < 1 || pitNumber > 10) return;
|
||
|
||
// 如果是单个展示模式且不是当前麦位,不处理
|
||
if (isSingleMode && this.currentSinglePit != pitNumber) return;
|
||
|
||
RoomSingSongWheatView wheatView = findWheatViewByPitNumber(pitNumber);
|
||
if (wheatView != null) {
|
||
RoomPitBean bean = pitBean;
|
||
wheatView.setData(bean); // 刷新数据
|
||
}
|
||
}
|
||
|
||
public void upDataCharm(RoomPitBean pitBean, int pitNumber){
|
||
// 检查容器状态
|
||
if (container == null || !isContainerValid()) {
|
||
return;
|
||
}
|
||
|
||
if (pitList == null || pitList.isEmpty() || pitNumber < 1 || pitNumber > 10) return;
|
||
|
||
// 如果是单个展示模式且不是当前麦位,不处理
|
||
if (isSingleMode && this.currentSinglePit != pitNumber) return;
|
||
|
||
RoomSingSongWheatView wheatView = findWheatViewByPitNumber(pitNumber);
|
||
if (wheatView != null) {
|
||
RoomPitBean bean = pitBean;
|
||
wheatView.setCharm(bean.getCharm()); // 刷新数据
|
||
}
|
||
}
|
||
|
||
@Nullable
|
||
private RoomSingSongWheatView findWheatViewByPitNumber(int pitNumber) {
|
||
// 检查容器状态
|
||
if (container == null || !isContainerValid()) {
|
||
return null;
|
||
}
|
||
|
||
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 RoomSingSongWheatView) {
|
||
RoomSingSongWheatView view = (RoomSingSongWheatView) child;
|
||
if (Integer.parseInt(view.pitNumber) == pitNumber) {
|
||
return view;
|
||
}
|
||
}
|
||
}
|
||
} else if (row instanceof RoomSingSongWheatView) {
|
||
RoomSingSongWheatView view = (RoomSingSongWheatView) row;
|
||
if (Integer.parseInt(view.pitNumber) == pitNumber) {
|
||
return view;
|
||
}
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
// 添加容器状态检查方法
|
||
private boolean isContainerValid() {
|
||
try {
|
||
// 检查容器是否已附加到窗口
|
||
return container != null && container.isAttachedToWindow();
|
||
} catch (Exception e) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 添加资源清理方法
|
||
public void release() {
|
||
try {
|
||
if (multiWheatViews!=null){
|
||
for (RoomSingSongWheatView wheatView : multiWheatViews) {
|
||
wheatView.releaseResources();
|
||
}
|
||
}
|
||
|
||
if (container != null) {
|
||
container.removeAllViews();
|
||
}
|
||
} catch (Exception e) {
|
||
// 忽略异常
|
||
}
|
||
|
||
// 清理引用
|
||
pitList = null;
|
||
singleWheatView = null;
|
||
wheatClickListener = null;
|
||
}
|
||
}
|