修改KTV房间编码模式
修改点唱房编码模式 优化展示麦位布局 优化Mqtt,变更成单例模式
This commit is contained in:
@@ -41,9 +41,9 @@
|
||||
<!-- android:exported="true" -->
|
||||
<!-- tools:ignore="Instantiatable" /> -->
|
||||
<!-- <service android:name=".service.EMqttService" />-->
|
||||
<service
|
||||
android:name=".service.MyMqttService"
|
||||
android:foregroundServiceType="dataSync" />
|
||||
<!-- <service-->
|
||||
<!-- android:name=".service.MyMqttService"-->
|
||||
<!-- android:foregroundServiceType="dataSync" />-->
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@@ -109,7 +109,14 @@ public class CommonAppContext extends MultiDexApplication {
|
||||
AgoraManager.getInstance(this).init(currentEnvironment.getSwSdkAppId());
|
||||
MessageListenerSingleton.getInstance();
|
||||
CrashReport.initCrashReport(this, "ac3ed4d89f", true);/*bugly初始化*/
|
||||
ServiceUtils.startService(MyMqttService.class);/*Mqtt初始化*/
|
||||
// ServiceUtils.startService(MyMqttService.class);/*Mqtt初始化*/
|
||||
// 初始化MQTT服务
|
||||
// 获取单例实例
|
||||
MyMqttService mqttService = MyMqttService.getInstance(this);
|
||||
|
||||
// 启动服务
|
||||
mqttService.startService();
|
||||
|
||||
// 每次启动应用时重置状态
|
||||
SpUtil.getInstance().setBooleanValue("youth_model_shown", false);
|
||||
}
|
||||
|
||||
@@ -1,27 +1,20 @@
|
||||
package com.xscm.moduleutil.service;
|
||||
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ServiceInfo;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.IBinder;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.blankj.utilcode.util.LogUtils;
|
||||
import com.blankj.utilcode.util.ServiceUtils;
|
||||
import com.orhanobut.logger.Logger;
|
||||
import com.xscm.moduleutil.R;
|
||||
import com.xscm.moduleutil.event.RoomGiftRunable;
|
||||
@@ -35,35 +28,32 @@ import org.eclipse.paho.client.mqttv3.MqttClient;
|
||||
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
||||
import org.eclipse.paho.client.mqttv3.MqttException;
|
||||
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
public class MyMqttService extends Service implements MyEmqttConnectListener, MyEmqttMesgListener, MyEmqttSubscribeListener {
|
||||
public class MyMqttService implements MyEmqttConnectListener, MyEmqttMesgListener, MyEmqttSubscribeListener {
|
||||
|
||||
private final static String TAG = "lxj";
|
||||
|
||||
private static int qos = 2;
|
||||
// private static String HOST ="tcp://81.70.45.221";//正式
|
||||
private static String HOST = "tcp://1.13.181.248";//测试
|
||||
// private static String HOST = "tcp://47.120.21.132";//测试
|
||||
private static MqttAndroidClient mqttAndroidClient;
|
||||
private MqttConnectOptions mMqttConnectOptions;
|
||||
private static boolean b = true;
|
||||
|
||||
// 使用单例模式
|
||||
private static MyMqttService instance;
|
||||
|
||||
private static MyEmqttConnectListener mMyEmqttConnectListener;
|
||||
private static MyEmqttMesgListener mMyEmqttMesgListener;
|
||||
private static MyEmqttSubscribeListener mMyEmqttSubscribeListener;
|
||||
// 使用线程安全的集合存储监听器
|
||||
private static final CopyOnWriteArrayList<MyEmqttMesgListener> messageListeners = new CopyOnWriteArrayList<>();
|
||||
private static final CopyOnWriteArrayList<MyEmqttConnectListener> connectListeners = new CopyOnWriteArrayList<>();
|
||||
private static final CopyOnWriteArrayList<MyEmqttSubscribeListener> subscribeListeners = new CopyOnWriteArrayList<>();
|
||||
|
||||
private static final String TOPIC_BOSS = "qx_room_topic";
|
||||
|
||||
private static final String TOPIC_XLH = "qx_xunlehui";
|
||||
private static final int NOTIFICATION_ID = 1;
|
||||
|
||||
|
||||
// 添加后台线程处理
|
||||
private HandlerThread mqttHandlerThread;
|
||||
@@ -74,27 +64,31 @@ public class MyMqttService extends Service implements MyEmqttConnectListener, My
|
||||
private static final int MAX_RETRY_ATTEMPTS = 5;
|
||||
private int retryCount = 0;
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { // API 31+
|
||||
// 设置前台服务类型为 "connectedDevice" 或其他合适类型
|
||||
setForegroundServiceBehavior(ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST);
|
||||
// 服务状态
|
||||
private static boolean isServiceRunning = false;
|
||||
|
||||
private Context mContext;
|
||||
|
||||
// 私有构造函数
|
||||
private MyMqttService(Context context) {
|
||||
this.mContext = context.getApplicationContext();
|
||||
initService();
|
||||
}
|
||||
|
||||
public static MyMqttService getInstance(Context context) {
|
||||
if (instance == null) {
|
||||
synchronized (MyMqttService.class) {
|
||||
if (instance == null) {
|
||||
instance = new MyMqttService(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
// ⚠️ 必须在这个方法开始就调用 startForeground()
|
||||
Notification notification = createNotification();
|
||||
startForeground(NOTIFICATION_ID, notification);
|
||||
|
||||
return START_STICKY;
|
||||
return instance;
|
||||
}
|
||||
|
||||
private void setForegroundServiceBehavior(int foregroundServiceTypeManifest) {
|
||||
// 空实现,仅用于兼容性处理
|
||||
}
|
||||
private void initService() {
|
||||
isServiceRunning = true;
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
// 创建专用的HandlerThread处理MQTT操作
|
||||
mqttHandlerThread = new HandlerThread("MqttServiceThread");
|
||||
mqttHandlerThread.start();
|
||||
@@ -105,77 +99,127 @@ public class MyMqttService extends Service implements MyEmqttConnectListener, My
|
||||
try {
|
||||
init();
|
||||
} catch (MqttException e) {
|
||||
throw new RuntimeException(e);
|
||||
Log.e(TAG, "MQTT初始化异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
private Notification createNotification() {
|
||||
// 创建你的前台通知
|
||||
// Notification.Builder builder = null;
|
||||
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
// builder = new Notification.Builder(this, "channel_id")
|
||||
// .setContentTitle("MQTT Service")
|
||||
// .setSmallIcon(R.mipmap.default_avatar);
|
||||
// }
|
||||
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
// NotificationChannel channel = new NotificationChannel(
|
||||
// "channel_id", "MQTT Channel",
|
||||
// NotificationManager.IMPORTANCE_LOW);
|
||||
// NotificationManager manager = getSystemService(NotificationManager.class);
|
||||
// manager.createNotificationChannel(channel);
|
||||
// }
|
||||
// return builder.build();
|
||||
|
||||
// 创建你的前台通知
|
||||
Notification.Builder builder = null;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
builder = new Notification.Builder(this, "mqtt_channel")
|
||||
.setContentTitle("消息服务")
|
||||
.setContentText("正在接收实时消息")
|
||||
.setSmallIcon(R.mipmap.default_avatar);
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
NotificationChannel channel = new NotificationChannel(
|
||||
"mqtt_channel", "MQTT Channel",
|
||||
NotificationManager.IMPORTANCE_LOW);
|
||||
NotificationManager manager = getSystemService(NotificationManager.class);
|
||||
if (manager != null) {
|
||||
manager.createNotificationChannel(channel);
|
||||
}
|
||||
}
|
||||
return builder != null ? builder.build() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 开启服务
|
||||
* 启动MQTT连接
|
||||
*/
|
||||
public static void startService(Context mContext) {
|
||||
public void startService() {
|
||||
b = true;
|
||||
boolean serviceRunning = ServiceUtils.isServiceRunning(MyMqttService.class.getCanonicalName());
|
||||
if (!serviceRunning) {
|
||||
mContext.startService(new Intent(mContext, MyMqttService.class));
|
||||
if (!isServiceRunning) {
|
||||
initService();
|
||||
}
|
||||
}
|
||||
|
||||
public static void stopService(Context context) {
|
||||
/**
|
||||
* 停止MQTT服务
|
||||
*/
|
||||
public void stopService() {
|
||||
b = false;
|
||||
boolean serviceRunning = ServiceUtils.isServiceRunning(MyMqttService.class.getCanonicalName());
|
||||
if (serviceRunning) {
|
||||
try {
|
||||
context.stopService(new Intent(context, MyMqttService.class));
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Failed to stop MQTT service", e);
|
||||
}
|
||||
isServiceRunning = false;
|
||||
cleanup();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
private void init() throws MqttException {
|
||||
String CLIENTID = "android-" + MqttClient.generateClientId();
|
||||
mqttAndroidClient = new MqttAndroidClient(mContext, HOST, CLIENTID);
|
||||
mqttAndroidClient.setCallback(mqttCallback); //设置监听订阅消息的回调
|
||||
mMqttConnectOptions = new MqttConnectOptions();
|
||||
mMqttConnectOptions.setCleanSession(true); //设置是否清除缓存
|
||||
mMqttConnectOptions.setConnectionTimeout(10); //设置超时时间,单位:秒
|
||||
mMqttConnectOptions.setKeepAliveInterval(10); //设置心跳包发送间隔,单位:秒
|
||||
mMqttConnectOptions.setUserName("public"); //设置用户名
|
||||
mMqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
|
||||
|
||||
if (mqttAndroidClient != null && !mqttAndroidClient.isConnected()) {
|
||||
doClientConnection();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
private void doClientConnection() throws MqttException {
|
||||
// 在后台线程执行连接操作
|
||||
mqttHandler.post(() -> {
|
||||
if (mqttAndroidClient != null && !mqttAndroidClient.isConnected() && isConnectIsNomarl() && b) {
|
||||
try {
|
||||
mqttAndroidClient.connect(mMqttConnectOptions, null, iMqttActionListener);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "MQTT连接异常", e);
|
||||
handleConnectionFailure();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void handleConnectionFailure() {
|
||||
retryCount++;
|
||||
if (retryCount < MAX_RETRY_ATTEMPTS) {
|
||||
// 延迟重试
|
||||
mqttHandler.postDelayed(() -> {
|
||||
if (b) {
|
||||
try {
|
||||
doClientConnection();
|
||||
} catch (MqttException e) {
|
||||
Log.e(TAG, "MQTT连接异常", e);
|
||||
}
|
||||
}
|
||||
}, 5000); // 5秒后重试
|
||||
} else {
|
||||
Log.w(TAG, "达到最大重试次数,停止重试");
|
||||
retryCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static void closeConnection() {
|
||||
Handler handler = new Handler(Looper.getMainLooper());
|
||||
handler.post(() -> {
|
||||
try {
|
||||
if (mqttAndroidClient != null && mqttAndroidClient.isConnected()) {
|
||||
IMqttToken disconnect = mqttAndroidClient.disconnect();
|
||||
disconnect.setActionCallback(new IMqttActionListener() {
|
||||
@Override
|
||||
public void onSuccess(IMqttToken asyncActionToken) {
|
||||
Logger.e(TAG, "断开链接", "断开链接成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
|
||||
Logger.e(TAG, "断开链接", "断开链接失败" + exception.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "关闭连接异常", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断网络是否连接
|
||||
*/
|
||||
private boolean isConnectIsNomarl() {
|
||||
try {
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
if (connectivityManager != null) {
|
||||
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
|
||||
if (info != null && info.isAvailable()) {
|
||||
String name = info.getTypeName();
|
||||
Log.i(TAG, "当前网络名称:" + name);
|
||||
return true;
|
||||
} else {
|
||||
Log.i(TAG, "没有可用网络");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "检查网络状态异常", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布 (模拟其他客户端发布消息)
|
||||
@@ -221,8 +265,10 @@ public class MyMqttService extends Service implements MyEmqttConnectListener, My
|
||||
subToken.setActionCallback(new IMqttActionListener() {
|
||||
@Override
|
||||
public void onSuccess(IMqttToken asyncActionToken) {
|
||||
if (mMyEmqttSubscribeListener != null) {
|
||||
mMyEmqttSubscribeListener.onSubscribeSuccess(topic);
|
||||
for (MyEmqttSubscribeListener listener : subscribeListeners) {
|
||||
if (listener != null) {
|
||||
listener.onSubscribeSuccess(topic);
|
||||
}
|
||||
}
|
||||
Logger.e(TAG, "订阅成功:" + topic);
|
||||
}
|
||||
@@ -230,9 +276,12 @@ public class MyMqttService extends Service implements MyEmqttConnectListener, My
|
||||
@Override
|
||||
public void onFailure(IMqttToken asyncActionToken,
|
||||
Throwable exception) {
|
||||
if ((!TOPIC_BOSS.equals(topic) && mMyEmqttSubscribeListener != null)
|
||||
|| (!TOPIC_XLH.equals(topic) && mMyEmqttSubscribeListener != null)) {
|
||||
mMyEmqttSubscribeListener.onSubscribeFailure();
|
||||
for (MyEmqttSubscribeListener listener : subscribeListeners) {
|
||||
if (listener != null &&
|
||||
!TOPIC_BOSS.equals(topic) &&
|
||||
!TOPIC_XLH.equals(topic)) {
|
||||
listener.onSubscribeFailure();
|
||||
}
|
||||
}
|
||||
Logger.e(TAG, "订阅失败:" + topic + ", error: " + exception.getMessage());
|
||||
}
|
||||
@@ -270,118 +319,20 @@ public class MyMqttService extends Service implements MyEmqttConnectListener, My
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*/
|
||||
private void init() throws MqttException {
|
||||
String CLIENTID = "android-" + MqttClient.generateClientId();
|
||||
mqttAndroidClient = new MqttAndroidClient(this, HOST, CLIENTID);
|
||||
mqttAndroidClient.setCallback(mqttCallback); //设置监听订阅消息的回调
|
||||
mMqttConnectOptions = new MqttConnectOptions();
|
||||
mMqttConnectOptions.setCleanSession(true); //设置是否清除缓存
|
||||
mMqttConnectOptions.setConnectionTimeout(10); //设置超时时间,单位:秒
|
||||
mMqttConnectOptions.setKeepAliveInterval(10); //设置心跳包发送间隔,单位:秒
|
||||
mMqttConnectOptions.setUserName("public"); //设置用户名
|
||||
// mMqttConnectOptions.setPassword(new char[0]); //设置密码
|
||||
mMqttConnectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
|
||||
|
||||
if (mqttAndroidClient != null && !mqttAndroidClient.isConnected()) {
|
||||
doClientConnection();
|
||||
}
|
||||
}
|
||||
|
||||
private void doClientConnection() throws MqttException {
|
||||
// 在后台线程执行连接操作
|
||||
mqttHandler.post(() -> {
|
||||
if (mqttAndroidClient != null && !mqttAndroidClient.isConnected() && isConnectIsNomarl() && b) {
|
||||
try {
|
||||
mqttAndroidClient.connect(mMqttConnectOptions, null, iMqttActionListener);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "MQTT连接异常", e);
|
||||
handleConnectionFailure();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void handleConnectionFailure() {
|
||||
retryCount++;
|
||||
if (retryCount < MAX_RETRY_ATTEMPTS) {
|
||||
// 延迟重试
|
||||
mqttHandler.postDelayed(() -> {
|
||||
if (b) {
|
||||
try {
|
||||
doClientConnection();
|
||||
} catch (MqttException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}, 5000); // 5秒后重试
|
||||
} else {
|
||||
Log.w(TAG, "达到最大重试次数,停止重试");
|
||||
retryCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static void closeConnection() {
|
||||
Handler handler = new Handler(Looper.getMainLooper());
|
||||
handler.post(() -> {
|
||||
try {
|
||||
if (mqttAndroidClient != null && mqttAndroidClient.isConnected()) {
|
||||
IMqttToken disconnect = mqttAndroidClient.disconnect();
|
||||
disconnect.setActionCallback(new IMqttActionListener() {
|
||||
@Override
|
||||
public void onSuccess(IMqttToken asyncActionToken) {
|
||||
Logger.e(TAG, "断开链接", "断开链接成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
|
||||
Logger.e(TAG, "断开链接", "断开链接失败" + exception.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "关闭连接异常", e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断网络是否连接
|
||||
*/
|
||||
private boolean isConnectIsNomarl() {
|
||||
try {
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager) this.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
if (connectivityManager != null) {
|
||||
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
|
||||
if (info != null && info.isAvailable()) {
|
||||
String name = info.getTypeName();
|
||||
Log.i(TAG, "当前网络名称:" + name);
|
||||
return true;
|
||||
} else {
|
||||
Log.i(TAG, "没有可用网络");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "检查网络状态异常", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
//MQTT是否连接成功的监听
|
||||
private IMqttActionListener iMqttActionListener = new IMqttActionListener() {
|
||||
|
||||
@Override
|
||||
public void onSuccess(IMqttToken arg0) {
|
||||
retryCount = 0; // 重置重试计数
|
||||
if (mMyEmqttConnectListener != null) {
|
||||
mMyEmqttConnectListener.onConnectSuccess();
|
||||
|
||||
// 通知所有连接监听器
|
||||
for (MyEmqttConnectListener listener : connectListeners) {
|
||||
if (listener != null) {
|
||||
listener.onConnectSuccess();
|
||||
}
|
||||
}
|
||||
|
||||
Logger.e(TAG, "链接状态:", "链接成功");
|
||||
subscribe(TOPIC_BOSS);
|
||||
subscribe(TOPIC_XLH);
|
||||
@@ -389,12 +340,15 @@ public class MyMqttService extends Service implements MyEmqttConnectListener, My
|
||||
|
||||
@Override
|
||||
public void onFailure(IMqttToken arg0, Throwable arg1) {
|
||||
if (mMyEmqttConnectListener != null) {
|
||||
mMyEmqttConnectListener.onConnectFailure();
|
||||
// 通知所有连接监听器
|
||||
for (MyEmqttConnectListener listener : connectListeners) {
|
||||
if (listener != null) {
|
||||
listener.onConnectFailure();
|
||||
}
|
||||
}
|
||||
// if (arg0 instanceof MqttException) {
|
||||
//// Logger.e(TAG, "链接状态:", "链接失败" + ((MqttException) arg1).getMessage());
|
||||
// }
|
||||
|
||||
Logger.e(TAG, "链接状态:", "链接失败: " + arg1.getMessage());
|
||||
|
||||
// 在后台线程处理重连
|
||||
mqttHandler.postDelayed(() -> {
|
||||
if (b) {
|
||||
@@ -404,7 +358,6 @@ public class MyMqttService extends Service implements MyEmqttConnectListener, My
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
//订阅主题的回调
|
||||
private MqttCallback mqttCallback = new MqttCallback() {
|
||||
|
||||
@@ -417,23 +370,29 @@ public class MyMqttService extends Service implements MyEmqttConnectListener, My
|
||||
Logger.e(TAG, "收到的消息", "主题:" + topic + " 收到的消息:" + messageStr);
|
||||
if (topic.equals(TOPIC_BOSS)) {
|
||||
// 处理消息
|
||||
receiveMessage(topic, messageStr);
|
||||
|
||||
// receiveMessage(topic, messageStr);
|
||||
// new Handler(Looper.getMainLooper()).post(() -> {
|
||||
receiveMessage(topic, messageStr);
|
||||
// });
|
||||
// 通知监听器
|
||||
if (mMyEmqttMesgListener != null) {
|
||||
// 切换到主线程通知
|
||||
new Handler(Looper.getMainLooper()).post(() -> {
|
||||
mMyEmqttMesgListener.messageArrived(topic, messageStr);
|
||||
});
|
||||
}
|
||||
// for (MyEmqttMesgListener listener : messageListeners) {
|
||||
// if (listener != null) {
|
||||
// // 切换到主线程通知
|
||||
//// new Handler(Looper.getMainLooper()).post(() -> {
|
||||
////// listener.messageArrived(topic, messageStr);
|
||||
//// });
|
||||
// }
|
||||
// }
|
||||
} else if (topic.equals(TOPIC_XLH)) {
|
||||
receiveXlhMessage(messageStr);
|
||||
// receiveXlhMessage(messageStr);
|
||||
// new Handler(Looper.getMainLooper()).post(() -> {
|
||||
receiveXlhMessage(messageStr);
|
||||
// });
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "处理MQTT消息异常", e);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -443,9 +402,13 @@ public class MyMqttService extends Service implements MyEmqttConnectListener, My
|
||||
|
||||
@Override
|
||||
public void connectionLost(Throwable arg0) {
|
||||
if (mMyEmqttConnectListener != null) {
|
||||
mMyEmqttConnectListener.onConnectFailure();
|
||||
// 通知所有连接监听器
|
||||
for (MyEmqttConnectListener listener : connectListeners) {
|
||||
if (listener != null) {
|
||||
listener.onConnectFailure();
|
||||
}
|
||||
}
|
||||
|
||||
Logger.e(TAG, "链接状态:", "链接断开: " + arg0.getMessage());
|
||||
|
||||
// 在后台线程处理重连
|
||||
@@ -454,7 +417,7 @@ public class MyMqttService extends Service implements MyEmqttConnectListener, My
|
||||
try {
|
||||
doClientConnection();
|
||||
} catch (MqttException e) {
|
||||
throw new RuntimeException(e);
|
||||
Log.e(TAG, "MQTT连接异常", e);
|
||||
}
|
||||
}
|
||||
}, 1000);
|
||||
@@ -463,14 +426,12 @@ public class MyMqttService extends Service implements MyEmqttConnectListener, My
|
||||
|
||||
private void receiveXlhMessage(String messageStr) {
|
||||
try {
|
||||
String newdata = messageStr;//TextLengthUtil.decode(data);
|
||||
String newdata = messageStr;
|
||||
JSONObject jsonObject = JSON.parseObject(newdata);
|
||||
|
||||
int type = jsonObject.getIntValue("type");
|
||||
String message = jsonObject.getString("msg");
|
||||
|
||||
|
||||
|
||||
// 将事件处理放到主线程执行
|
||||
new Handler(Looper.getMainLooper()).post(() -> {
|
||||
processMessageType(type, message);
|
||||
@@ -482,7 +443,7 @@ public class MyMqttService extends Service implements MyEmqttConnectListener, My
|
||||
|
||||
private void receiveMessage(String topic, String data) {
|
||||
try {
|
||||
String newdata = data;//TextLengthUtil.decode(data);
|
||||
String newdata = data;
|
||||
JSONObject jsonObject = JSON.parseObject(newdata);
|
||||
|
||||
int type = jsonObject.getIntValue("type");
|
||||
@@ -644,10 +605,10 @@ public class MyMqttService extends Service implements MyEmqttConnectListener, My
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
b = false;
|
||||
|
||||
/**
|
||||
* 清理资源
|
||||
*/
|
||||
public void cleanup() {
|
||||
try {
|
||||
// 清理资源
|
||||
if (messageExecutorService != null) {
|
||||
@@ -658,6 +619,7 @@ public class MyMqttService extends Service implements MyEmqttConnectListener, My
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
messageExecutorService.shutdownNow();
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -672,51 +634,65 @@ public class MyMqttService extends Service implements MyEmqttConnectListener, My
|
||||
mqttAndroidClient.unregisterResources();
|
||||
mqttAndroidClient = null;
|
||||
}
|
||||
stopForeground(true); // 停止前台服务
|
||||
Logger.e(TAG, "服务关闭", "资源释放成功");
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "服务关闭异常", e);
|
||||
}
|
||||
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
|
||||
// 修改监听器管理方法
|
||||
public static void addMyEmqttMesgListener(MyEmqttMesgListener myEmqttMesgListener) {
|
||||
mMyEmqttMesgListener = myEmqttMesgListener;
|
||||
if (myEmqttMesgListener != null && !messageListeners.contains(myEmqttMesgListener)) {
|
||||
messageListeners.add(myEmqttMesgListener);
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeMyEmqttMesgListener(MyEmqttMesgListener myEmqttMesgListener) {
|
||||
messageListeners.remove(myEmqttMesgListener);
|
||||
}
|
||||
|
||||
public static void addMyEmqttConnectListener(MyEmqttConnectListener myEmqttConnectListener) {
|
||||
mMyEmqttConnectListener = myEmqttConnectListener;
|
||||
if (myEmqttConnectListener != null && !connectListeners.contains(myEmqttConnectListener)) {
|
||||
connectListeners.add(myEmqttConnectListener);
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeMyEmqttConnectListener(MyEmqttConnectListener myEmqttConnectListener) {
|
||||
connectListeners.remove(myEmqttConnectListener);
|
||||
}
|
||||
|
||||
public static void addMyEmqttSubscribeListener(MyEmqttSubscribeListener myEmqttSubscribeListener) {
|
||||
mMyEmqttSubscribeListener = myEmqttSubscribeListener;
|
||||
if (myEmqttSubscribeListener != null && !subscribeListeners.contains(myEmqttSubscribeListener)) {
|
||||
subscribeListeners.add(myEmqttSubscribeListener);
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeMyEmqttSubscribeListener(MyEmqttSubscribeListener myEmqttSubscribeListener) {
|
||||
subscribeListeners.remove(myEmqttSubscribeListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectSuccess() {
|
||||
|
||||
// 实现接口方法
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectFailure() {
|
||||
|
||||
// 实现接口方法
|
||||
}
|
||||
|
||||
@Override
|
||||
public void messageArrived(String topic, String mesg) {
|
||||
LogUtils.e("lxj", "messageArrived:"+mesg);
|
||||
LogUtils.e("lxj", "messageArrived:" + mesg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubscribeSuccess(String topic) {
|
||||
LogUtils.e("lxj", "onSubscribeSuccess:"+topic);
|
||||
LogUtils.e("lxj", "onSubscribeSuccess:" + topic);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubscribeFailure() {
|
||||
LogUtils.e("lxj", "onSubscribeFailure");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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