修改KTV房间编码模式
修改点唱房编码模式 优化展示麦位布局 优化Mqtt,变更成单例模式
This commit is contained in:
@@ -40,9 +40,15 @@ public class RoomSingSongWheatView extends BaseWheatView {
|
||||
|
||||
@Override
|
||||
protected void initPit(Context context, AttributeSet attrs) {
|
||||
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoomDefaultWheatView);
|
||||
pitNumber = typedArray.getString(R.styleable.RoomDefaultWheatView_room_wheat_number);
|
||||
typedArray.recycle();
|
||||
TypedArray typedArray = null;
|
||||
try {
|
||||
typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoomDefaultWheatView);
|
||||
pitNumber = typedArray.getString(R.styleable.RoomDefaultWheatView_room_wheat_number);
|
||||
} finally {
|
||||
if (typedArray != null) {
|
||||
typedArray.recycle();
|
||||
}
|
||||
}
|
||||
mIvTagBoss = findViewById(R.id.iv_tag_boos);
|
||||
mTvTime = findViewById(R.id.tv_time);
|
||||
tv_time_pk = findViewById(R.id.tv_time_pk);
|
||||
@@ -55,83 +61,96 @@ public class RoomSingSongWheatView extends BaseWheatView {
|
||||
|
||||
@Override
|
||||
protected void setPitData(RoomPitBean bean) {
|
||||
if (bean == null) return;
|
||||
|
||||
sex = bean.getSex();
|
||||
pitBean = bean; // 统一使用参数 bean
|
||||
|
||||
if (isOn()) {
|
||||
//开启声浪
|
||||
mIvRipple.stopAnimation();
|
||||
mIvRipple.setVisibility(VISIBLE);
|
||||
mTvName.setText(bean.getNickname());
|
||||
ImageUtils.loadHeadCC(bean.getAvatar(), mRiv);
|
||||
if (TextUtils.isEmpty(pitBean.getDress())) {
|
||||
mIvFrame.setVisibility(INVISIBLE);
|
||||
} else {
|
||||
mIvFrame.setVisibility(VISIBLE);
|
||||
mIvFrame.setSource(pitBean.getDress(), 1);
|
||||
// ImageUtils.loadDecorationAvatar(pitBean.getDress_picture(), mIvFrame);
|
||||
}
|
||||
if (showBoss && WHEAT_BOSS.equals(pitNumber)) {
|
||||
mIvTagBoss.setVisibility(GONE);
|
||||
}
|
||||
handleOnState(bean);
|
||||
} else {
|
||||
mTvName.setText(
|
||||
"-1".equals(pitNumber) ? "" :
|
||||
"9".equals(pitNumber) ? "主持位" :
|
||||
"10".equals(pitNumber) ? "嘉宾位" :
|
||||
pitNumber + "号麦位"
|
||||
);
|
||||
//麦位上锁
|
||||
if (showBoss && WHEAT_BOSS.equals(pitNumber)) {
|
||||
mIvTagBoss.setVisibility(VISIBLE);
|
||||
ImageUtils.loadRes(isLocked() ? R.mipmap.room_ic_wheat_default_suo : R.mipmap.room_ic_wheat_default, mRiv);
|
||||
} else {
|
||||
// mIvTagBoss.setVisibility(GONE);
|
||||
// @DrawableRes int origin = getOriginImage();
|
||||
// ImageUtils.loadRes(isLocked() ? R.mipmap.room_ic_wheat_default_suo :
|
||||
// (origin == 0 ? R.mipmap.room_ic_wheat_default : origin), mRiv);
|
||||
mRiv.setImageResource(bean.getIs_lock() == 1 ? R.mipmap.room_ic_wheat_default_suo : R.mipmap.room_ic_wheat_default);
|
||||
// ImageUtils.loadRes(isLocked() ? R.mipmap.room_ic_wheat_default_suo : R.mipmap.room_ic_wheat_default, mRiv);
|
||||
}
|
||||
if (isMute()) {
|
||||
ImageUtils.loadRes(R.mipmap.room_microphone_off, mIvSex);
|
||||
}
|
||||
mIvFrame.setVisibility(INVISIBLE);
|
||||
mIvFace.remove();
|
||||
//停止声浪
|
||||
mIvRipple.stopAnimation();
|
||||
mIvRipple.setVisibility(GONE);
|
||||
handleOffState(bean);
|
||||
}
|
||||
|
||||
updateSexIcon();
|
||||
updateCharmViewVisibility(bean);
|
||||
updatePkState(bean);
|
||||
}
|
||||
|
||||
private void handleOnState(RoomPitBean bean) {
|
||||
stopAndClearAnimation(); // 清理之前的动画资源
|
||||
mIvRipple.setVisibility(VISIBLE);
|
||||
mTvName.setText(bean.getNickname());
|
||||
ImageUtils.loadHeadCC(bean.getAvatar(), mRiv);
|
||||
|
||||
if (TextUtils.isEmpty(bean.getDress())) {
|
||||
mIvFrame.setVisibility(INVISIBLE);
|
||||
} else {
|
||||
mIvFrame.setVisibility(VISIBLE);
|
||||
mIvFrame.setSource(bean.getDress(), 1);
|
||||
}
|
||||
|
||||
if (showBoss && TextUtils.equals(WHEAT_BOSS, pitNumber)) {
|
||||
mIvTagBoss.setVisibility(GONE);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleOffState(RoomPitBean bean) {
|
||||
stopAndClearAnimation(); // 下麦时停止并清理动画
|
||||
mTvName.setText(getPitNumberText());
|
||||
|
||||
if (showBoss && TextUtils.equals(WHEAT_BOSS, pitNumber)) {
|
||||
mIvTagBoss.setVisibility(VISIBLE);
|
||||
int resId = bean.getIs_lock() == 1 ? R.mipmap.room_ic_wheat_default_suo : R.mipmap.room_ic_wheat_default;
|
||||
mRiv.setImageResource(resId);
|
||||
} else {
|
||||
mRiv.setImageResource(bean.getIs_lock() == 1 ? R.mipmap.room_ic_wheat_default_suo : R.mipmap.room_ic_wheat_default);
|
||||
}
|
||||
|
||||
if (isMute()) {
|
||||
ImageUtils.loadRes(R.mipmap.room_microphone_off, mIvSex);
|
||||
}
|
||||
|
||||
mIvFrame.setVisibility(INVISIBLE);
|
||||
mIvFace.remove();
|
||||
mIvRipple.setVisibility(GONE);
|
||||
}
|
||||
|
||||
private String getPitNumberText() {
|
||||
if ("-1".equals(pitNumber)) return "";
|
||||
if ("9".equals(pitNumber)) return "主持位";
|
||||
if ("10".equals(pitNumber)) return "嘉宾位";
|
||||
return pitNumber + "号麦位";
|
||||
}
|
||||
|
||||
private void updateSexIcon() {
|
||||
if (showSexIcon) {
|
||||
checkSex();
|
||||
}
|
||||
if (pitBean.getNickname() == null || pitBean.getNickname().isEmpty()) {
|
||||
mCharmView.setVisibility(GONE);
|
||||
} else {
|
||||
mCharmView.setVisibility(VISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
if (pitBean.is_pk() ){
|
||||
if (pitBean.getUser_id()!=null && !pitBean.getUser_id().equals("0") && !pitBean.getUser_id().isEmpty()) {
|
||||
private void updateCharmViewVisibility(RoomPitBean bean) {
|
||||
boolean isEmptyNickname = bean.getNickname() == null || bean.getNickname().isEmpty();
|
||||
mCharmView.setVisibility(isEmptyNickname ? GONE : VISIBLE);
|
||||
}
|
||||
|
||||
private void updatePkState(RoomPitBean bean) {
|
||||
if (bean.is_pk()) {
|
||||
String userId = bean.getUser_id();
|
||||
if (userId != null && !userId.equals("0") && !userId.isEmpty()) {
|
||||
tv_time_pk.setVisibility(VISIBLE);
|
||||
setSex(pitBean.getCharm(),false);
|
||||
setSex(bean.getCharm(), false);
|
||||
mCharmView.setVisibility(GONE);
|
||||
}else {
|
||||
} else {
|
||||
tv_time_pk.setVisibility(GONE);
|
||||
}
|
||||
// ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mCharmView.getLayoutParams();
|
||||
// params.width = 35;
|
||||
// mCharmView.setLayoutParams(params);
|
||||
|
||||
}else {
|
||||
} else {
|
||||
tv_time_pk.setVisibility(GONE);
|
||||
mCharmView.setVisibility(VISIBLE);
|
||||
// ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mCharmView.getLayoutParams();
|
||||
// params.width = 52;
|
||||
// mCharmView.setLayoutParams(params);
|
||||
}
|
||||
|
||||
// setCardiac(pitBean.getPit_number(), 0.0f);
|
||||
}
|
||||
public void setSex( String value, boolean format) {
|
||||
|
||||
public void setSex(String value, boolean format) {
|
||||
if (format) {
|
||||
tv_time_pk.setText(value);
|
||||
} else {
|
||||
@@ -139,12 +158,12 @@ public class RoomSingSongWheatView extends BaseWheatView {
|
||||
long xd = Long.parseLong(value);
|
||||
if (xd > 9999 || xd < -9999) {
|
||||
tv_time_pk.setText(String.format("%.2fw", xd / 10000.0f));
|
||||
// mBinding.tvValue.setText(String.valueOf(xd));
|
||||
} else {
|
||||
tv_time_pk.setText(value);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
tv_time_pk.setText("0"); // 设置默认值防止UI异常
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -210,38 +229,91 @@ public class RoomSingSongWheatView extends BaseWheatView {
|
||||
if (maozi != null) maozi.setVisibility(GONE);
|
||||
}
|
||||
|
||||
// 添加内存释放方法
|
||||
public void releaseResources() {
|
||||
stopAndClearAnimation();
|
||||
|
||||
// 清理头像加载
|
||||
if (mRiv != null) {
|
||||
mRiv.setImageBitmap(null);
|
||||
}
|
||||
|
||||
// 清理框架视图
|
||||
if (mIvFrame != null) {
|
||||
mIvFrame.release(); // 清理SVGA资源
|
||||
}
|
||||
|
||||
// 清理表情视图
|
||||
if (mIvFace != null) {
|
||||
mIvFace.remove();
|
||||
}
|
||||
|
||||
// 清理其他图片资源
|
||||
if (mIvSex != null) {
|
||||
mIvSex.setImageBitmap(null);
|
||||
}
|
||||
|
||||
if (mIvTagBoss != null) {
|
||||
mIvTagBoss.setImageBitmap(null);
|
||||
}
|
||||
}
|
||||
|
||||
// 停止并清理动画资源
|
||||
private void stopAndClearAnimation() {
|
||||
if (mIvRipple != null) {
|
||||
mIvRipple.stopAnimation();
|
||||
// 清理SVGA资源,避免内存泄漏
|
||||
mIvRipple.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDetachedFromWindow() {
|
||||
// 视图从窗口分离时释放资源
|
||||
releaseResources();
|
||||
super.onDetachedFromWindow();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRemoteSoundLevelUpdate(String userId, int soundLevel) {
|
||||
|
||||
// 暂无实现
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLocalSoundLevelUpdate(int volume) {
|
||||
if (volume==0){
|
||||
mIvRipple.stopAnimation();
|
||||
} else {
|
||||
if (pitBean.getUser_id().equals(SpUtil.getUserId()) && closePhone) {
|
||||
if (volume == 0) {
|
||||
if (mIvRipple != null) {
|
||||
mIvRipple.stopAnimation();
|
||||
}else {
|
||||
mIvRipple.post(() -> {
|
||||
if (!mIvRipple.isAnimating()) {
|
||||
mIvRipple.startAnimation();
|
||||
}
|
||||
mIvRipple.setVisibility(VISIBLE);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// 增加空指针检查
|
||||
if (pitBean != null && pitBean.getUser_id() != null &&
|
||||
pitBean.getUser_id().equals(SpUtil.getUserId()) && closePhone) {
|
||||
if (mIvRipple != null) {
|
||||
mIvRipple.stopAnimation();
|
||||
}
|
||||
} else {
|
||||
if (mIvRipple != null) {
|
||||
mIvRipple.post(() -> {
|
||||
if (mIvRipple != null && !mIvRipple.isAnimating()) {
|
||||
mIvRipple.startAnimation();
|
||||
}
|
||||
if (mIvRipple != null) {
|
||||
mIvRipple.setVisibility(VISIBLE);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void userJoined(int userId, int elapsd) {
|
||||
|
||||
// 暂无实现
|
||||
}
|
||||
|
||||
@Override
|
||||
public void userOffline(int userId, int reason) {
|
||||
|
||||
// 暂无实现
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,13 +22,12 @@ public class WheatLayoutManager {
|
||||
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;
|
||||
@@ -39,11 +38,13 @@ public class WheatLayoutManager {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -54,19 +55,15 @@ public class WheatLayoutManager {
|
||||
|
||||
public void showSingleWheat(int pitNumber) {
|
||||
if (isSingleMode && this.currentSinglePit == pitNumber) return;
|
||||
if (pitNumber < 1 || pitNumber > 10 || pitList == null || pitList.size() < 10) 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
|
||||
@@ -74,7 +71,6 @@ public class WheatLayoutManager {
|
||||
params.setMargins(20, 20, 20, 20);
|
||||
singleWheatView.setLayoutParams(params);
|
||||
|
||||
// 添加点击事件
|
||||
singleWheatView.setOnClickListener(v -> {
|
||||
if (wheatClickListener != null) {
|
||||
wheatClickListener.onWheatClick(singleWheatView, pitNumber);
|
||||
@@ -91,42 +87,24 @@ public class WheatLayoutManager {
|
||||
container.removeAllViews();
|
||||
|
||||
int screenWidth = getScreenWidth();
|
||||
int itemWidth = screenWidth / 4; // 每个控件宽度为屏幕宽度的 1/4
|
||||
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 = new RoomDefaultWheatView(context);
|
||||
wheatView.pitNumber = String.valueOf(pitNumber);
|
||||
wheatView.setData(pitList.get(pitNumber - 1));
|
||||
RoomDefaultWheatView wheatView = createWheatView(pitNumber);
|
||||
|
||||
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
|
||||
if (i == 0 || i == 1) {
|
||||
int fixedHeightInPx = dpToPx(110);
|
||||
params = new LinearLayout.LayoutParams(itemWidth, fixedHeightInPx);
|
||||
if (i == 0) params.rightMargin = dpToPx(50);
|
||||
} else {
|
||||
int fixedHeightInDp = 90; // 固定高度为 100dp
|
||||
int fixedHeightInPx = dpToPx(fixedHeightInDp); // 调用已有的 dpToPx 方法
|
||||
int fixedHeightInPx = dpToPx(90);
|
||||
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 控制
|
||||
params.setMargins(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
wheatView.setLayoutParams(params);
|
||||
@@ -134,143 +112,48 @@ public class WheatLayoutManager {
|
||||
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) {
|
||||
if (i == 1 || (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();
|
||||
}
|
||||
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;
|
||||
}
|
||||
int firstPitNumber = layoutType == 1 ? 10 : 9;
|
||||
int secondPitNumber = layoutType == 1 ? 9 : 10;
|
||||
|
||||
// 添加第一个控件(10 或 9)
|
||||
addWheatViewItem(row, firstPitNumber, itemWidth*2, layoutType);
|
||||
|
||||
// 添加第二个控件(9 或 10)
|
||||
addWheatViewItem(row, secondPitNumber, itemWidth*2, layoutType);
|
||||
addWheatViewItem(row, firstPitNumber, itemWidth * 2, layoutType);
|
||||
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) {
|
||||
if ((i - 2) % 4 == 3) {
|
||||
container.addView(row);
|
||||
row = new LinearLayout(context);
|
||||
row.setOrientation(LinearLayout.HORIZONTAL);
|
||||
@@ -285,43 +168,29 @@ public class WheatLayoutManager {
|
||||
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));
|
||||
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);
|
||||
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
|
||||
} 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) {
|
||||
// 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);
|
||||
} 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);
|
||||
@@ -331,9 +200,9 @@ public class WheatLayoutManager {
|
||||
wheatView.setLayoutParams(params);
|
||||
wheatView.setOnClickListener(v -> {
|
||||
if (wheatClickListener != null) {
|
||||
if (layoutType==1) {
|
||||
if (layoutType == 1) {
|
||||
wheatClickListener.onWheatClick(wheatView, Integer.parseInt(wheatView.pitNumber));
|
||||
}else {
|
||||
} else {
|
||||
wheatClickListener.onMakeWheatClick(wheatView, Integer.parseInt(wheatView.pitNumber));
|
||||
}
|
||||
}
|
||||
@@ -342,7 +211,6 @@ public class WheatLayoutManager {
|
||||
row.addView(wheatView);
|
||||
}
|
||||
|
||||
|
||||
private RoomDefaultWheatView createWheatView(int pitNumber) {
|
||||
RoomDefaultWheatView wheatView = new RoomDefaultWheatView(context);
|
||||
wheatView.pitNumber = String.valueOf(pitNumber);
|
||||
@@ -350,14 +218,6 @@ public class WheatLayoutManager {
|
||||
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);
|
||||
}
|
||||
@@ -367,21 +227,13 @@ public class WheatLayoutManager {
|
||||
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); // 刷新数据
|
||||
wheatView.setData(pitBean);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -410,22 +262,37 @@ public class WheatLayoutManager {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量刷新多个麦位状态
|
||||
*/
|
||||
public void refreshWheatData(List<RoomPitBean> newPitList, List<Integer> changedPits) {
|
||||
this.pitList = newPitList;
|
||||
for (int pitNumber : changedPits) {
|
||||
// updateSingleWheat(pitNumber);
|
||||
updateSingleWheat(pitList.get(pitNumber - 1), pitNumber);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateSingleOnlineWheat(UserOnlineStatusBean bean) {
|
||||
if (pitList == null || pitList.isEmpty()) return;
|
||||
if (pitList == null || pitList.isEmpty() || bean == null) return;
|
||||
|
||||
for (RoomPitBean pitBean : pitList) {
|
||||
int pitNumber = Integer.parseInt(pitBean.getPit_number());
|
||||
RoomDefaultWheatView wheatView = findWheatViewByPitNumber(pitNumber);
|
||||
wheatView.setOnlineStatus(bean); // 刷新数据
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,11 +44,21 @@ public class WheatLayoutSingManager {
|
||||
}
|
||||
|
||||
public void setWheatData(List<RoomPitBean> pitList) {
|
||||
// 检查容器状态
|
||||
if (container == null || !isContainerValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.pitList = pitList;
|
||||
restoreMultiWheat();
|
||||
}
|
||||
|
||||
public void setWheatDataPk(List<RoomPitBean> pitList, int layoutType) {
|
||||
// 检查容器状态
|
||||
if (container == null || !isContainerValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.pitList = pitList;
|
||||
restoreMultiWheatPk(layoutType);
|
||||
}
|
||||
@@ -58,9 +68,19 @@ public class WheatLayoutSingManager {
|
||||
}
|
||||
|
||||
public void showSingleWheat(int pitNumber) {
|
||||
// 检查容器状态
|
||||
if (container == null || !isContainerValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isSingleMode && this.currentSinglePit == pitNumber) return;
|
||||
|
||||
container.removeAllViews();
|
||||
try {
|
||||
container.removeAllViews();
|
||||
} catch (Exception e) {
|
||||
// 忽略异常,继续执行
|
||||
return;
|
||||
}
|
||||
|
||||
if (pitNumber < 1 || pitNumber > 10 || pitList == null || pitList.size() < 10)
|
||||
return;
|
||||
@@ -93,7 +113,21 @@ public class WheatLayoutSingManager {
|
||||
}
|
||||
|
||||
public void restoreMultiWheat() {
|
||||
container.removeAllViews();
|
||||
// 检查容器状态
|
||||
if (container == null || !isContainerValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
container.removeAllViews();
|
||||
} catch (Exception e) {
|
||||
// 忽略异常,继续执行
|
||||
return;
|
||||
}
|
||||
|
||||
if (pitList == null || pitList.size() < 10) {
|
||||
return;
|
||||
}
|
||||
|
||||
int screenWidth = getScreenWidth();
|
||||
int itemWidth = screenWidth / 4; // 每个控件宽度为屏幕宽度的 1/4
|
||||
@@ -126,11 +160,6 @@ public class WheatLayoutSingManager {
|
||||
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 控制
|
||||
}
|
||||
|
||||
@@ -139,7 +168,6 @@ public class WheatLayoutSingManager {
|
||||
if (wheatClickListener != null) {
|
||||
wheatClickListener.onWheatClick(wheatView, Integer.parseInt(wheatView.pitNumber));
|
||||
}
|
||||
// showSingleWheat(Integer.parseInt(wheatView.pitNumber));
|
||||
});
|
||||
|
||||
row.addView(wheatView);
|
||||
@@ -156,91 +184,33 @@ public class WheatLayoutSingManager {
|
||||
}
|
||||
}
|
||||
|
||||
// 添加最后一行可能存在的剩余 view
|
||||
// 添加最后一行可能存在的剩余 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();
|
||||
// 检查容器状态
|
||||
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;
|
||||
|
||||
@@ -292,6 +262,11 @@ public class WheatLayoutSingManager {
|
||||
|
||||
// 抽取公共方法:添加单个控件
|
||||
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));
|
||||
@@ -317,11 +292,9 @@ public class WheatLayoutSingManager {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -347,7 +320,6 @@ public class WheatLayoutSingManager {
|
||||
row.addView(wheatView);
|
||||
}
|
||||
|
||||
|
||||
private RoomSingSongWheatView createWheatView(int pitNumber) {
|
||||
RoomSingSongWheatView wheatView = new RoomSingSongWheatView(context);
|
||||
wheatView.pitNumber = String.valueOf(pitNumber);
|
||||
@@ -362,7 +334,6 @@ public class WheatLayoutSingManager {
|
||||
return wheatView;
|
||||
}
|
||||
|
||||
|
||||
private int dpToPx(int dp) {
|
||||
return Math.round(dp * context.getResources().getDisplayMetrics().density);
|
||||
}
|
||||
@@ -376,6 +347,11 @@ public class WheatLayoutSingManager {
|
||||
* 更新指定 pitNumber 的麦位信息(用于局部刷新)
|
||||
*/
|
||||
public void updateSingleWheat(RoomPitBean pitBean, int pitNumber) {
|
||||
// 检查容器状态
|
||||
if (container == null || !isContainerValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (pitList == null || pitList.isEmpty() || pitNumber < 1 || pitNumber > 10) return;
|
||||
|
||||
// 如果是单个展示模式且不是当前麦位,不处理
|
||||
@@ -383,8 +359,6 @@ public class WheatLayoutSingManager {
|
||||
|
||||
RoomSingSongWheatView wheatView = findWheatViewByPitNumber(pitNumber);
|
||||
if (wheatView != null) {
|
||||
|
||||
// RoomPitBean bean = pitList.get(pitNumber - 1);
|
||||
RoomPitBean bean = pitBean;
|
||||
wheatView.setData(bean); // 刷新数据
|
||||
}
|
||||
@@ -392,6 +366,11 @@ public class WheatLayoutSingManager {
|
||||
|
||||
@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) {
|
||||
@@ -419,21 +398,73 @@ public class WheatLayoutSingManager {
|
||||
* 批量刷新多个麦位状态
|
||||
*/
|
||||
public void refreshWheatData(List<RoomPitBean> newPitList, List<Integer> changedPits) {
|
||||
this.pitList = newPitList;
|
||||
for (int pitNumber : changedPits) {
|
||||
// updateSingleWheat(pitNumber);
|
||||
// 检查容器状态
|
||||
if (container == null || !isContainerValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.pitList = newPitList;
|
||||
// 注意:原代码中此方法体为空,如果需要实现请取消注释下面的代码
|
||||
// for (int pitNumber : changedPits) {
|
||||
// updateSingleWheat(pitNumber);
|
||||
// }
|
||||
}
|
||||
|
||||
public void updateSingleOnlineWheat(UserOnlineStatusBean bean) {
|
||||
// 检查容器状态
|
||||
if (container == null || !isContainerValid()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (pitList == null || pitList.isEmpty()) return;
|
||||
|
||||
for (RoomPitBean pitBean : pitList) {
|
||||
int pitNumber = Integer.parseInt(pitBean.getPit_number());
|
||||
RoomSingSongWheatView wheatView = findWheatViewByPitNumber(pitNumber);
|
||||
wheatView.setOnlineStatus(bean); // 刷新数据
|
||||
if (wheatView != null) {
|
||||
wheatView.setOnlineStatus(bean); // 刷新数据
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 添加容器状态检查方法
|
||||
private boolean isContainerValid() {
|
||||
try {
|
||||
// 检查容器是否已附加到窗口
|
||||
return container != null && container.isAttachedToWindow();
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 添加资源清理方法
|
||||
public void release() {
|
||||
try {
|
||||
if (container != null) {
|
||||
// 清理所有子视图的资源
|
||||
for (int i = 0; i < container.getChildCount(); i++) {
|
||||
View child = container.getChildAt(i);
|
||||
if (child instanceof LinearLayout) {
|
||||
LinearLayout linearLayout = (LinearLayout) child;
|
||||
for (int j = 0; j < linearLayout.getChildCount(); j++) {
|
||||
View view = linearLayout.getChildAt(j);
|
||||
if (view instanceof RoomSingSongWheatView) {
|
||||
((RoomSingSongWheatView) view).releaseResources();
|
||||
}
|
||||
}
|
||||
} else if (child instanceof RoomSingSongWheatView) {
|
||||
((RoomSingSongWheatView) child).releaseResources();
|
||||
}
|
||||
}
|
||||
container.removeAllViews();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 忽略异常
|
||||
}
|
||||
|
||||
// 清理引用
|
||||
pitList = null;
|
||||
singleWheatView = null;
|
||||
wheatClickListener = null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user