2025-10-28 16:56:13 +08:00
|
|
|
|
package com.xscm.modulemain.adapter;
|
2025-10-20 10:16:44 +08:00
|
|
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2025-10-24 17:52:11 +08:00
|
|
|
|
import android.content.Context;
|
|
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
|
import android.graphics.Color;
|
|
|
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
|
|
import android.os.Build;
|
|
|
|
|
|
import android.os.Handler;
|
|
|
|
|
|
import android.text.Spannable;
|
|
|
|
|
|
import android.text.SpannableStringBuilder;
|
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
import android.text.style.ForegroundColorSpan;
|
2025-10-20 10:16:44 +08:00
|
|
|
|
import android.view.View;
|
|
|
|
|
|
import android.widget.ImageView;
|
|
|
|
|
|
import android.widget.LinearLayout;
|
2025-11-14 17:38:30 +08:00
|
|
|
|
import android.widget.TextView;
|
2025-10-20 10:16:44 +08:00
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
2025-10-24 17:52:11 +08:00
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
|
import androidx.constraintlayout.widget.ConstraintLayout;
|
2025-10-28 16:56:13 +08:00
|
|
|
|
|
2025-11-14 17:38:30 +08:00
|
|
|
|
import com.blankj.utilcode.util.LogUtils;
|
2025-12-05 16:41:32 +08:00
|
|
|
|
import com.blankj.utilcode.util.ToastUtils;
|
2025-10-20 10:16:44 +08:00
|
|
|
|
import com.bumptech.glide.Glide;
|
2025-10-24 17:52:11 +08:00
|
|
|
|
import com.bumptech.glide.load.DataSource;
|
|
|
|
|
|
import com.bumptech.glide.load.engine.GlideException;
|
|
|
|
|
|
import com.bumptech.glide.request.RequestListener;
|
|
|
|
|
|
import com.bumptech.glide.request.target.SimpleTarget;
|
|
|
|
|
|
import com.bumptech.glide.request.target.Target;
|
|
|
|
|
|
import com.bumptech.glide.request.transition.Transition;
|
2025-10-20 10:16:44 +08:00
|
|
|
|
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
|
|
|
|
|
|
import com.chad.library.adapter.base.BaseViewHolder;
|
2025-10-24 17:52:11 +08:00
|
|
|
|
import com.xscm.modulemain.FakeNinePatchDrawable;
|
2025-11-14 17:38:30 +08:00
|
|
|
|
import com.xscm.modulemain.R;
|
2025-10-24 17:52:11 +08:00
|
|
|
|
import com.xscm.moduleutil.bean.GiftBean;
|
2025-10-20 10:16:44 +08:00
|
|
|
|
import com.xscm.moduleutil.bean.RoomMessageEvent;
|
2025-10-24 17:52:11 +08:00
|
|
|
|
import com.xscm.moduleutil.bean.UserInfo;
|
2025-10-20 10:16:44 +08:00
|
|
|
|
import com.xscm.moduleutil.bean.room.EMMessageInfo;
|
2026-01-08 19:30:21 +08:00
|
|
|
|
import com.xscm.moduleutil.rtc.AgoraManager;
|
2025-10-20 10:16:44 +08:00
|
|
|
|
import com.xscm.moduleutil.utils.ImageUtils;
|
2025-11-12 16:42:51 +08:00
|
|
|
|
import com.xscm.moduleutil.utils.MeHeadView;
|
2025-10-25 18:07:21 +08:00
|
|
|
|
import com.xscm.moduleutil.widget.AdaptiveImageView;
|
2025-11-14 17:38:30 +08:00
|
|
|
|
import com.xscm.moduleutil.widget.ShineTextView;
|
2025-10-24 17:52:11 +08:00
|
|
|
|
import com.xscm.moduleutil.widget.img.BubbleBackgroundHelper;
|
2025-11-14 17:38:30 +08:00
|
|
|
|
import com.xscm.moduleutil.bean.room.EmotionDeatils;
|
2025-10-20 10:16:44 +08:00
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
|
import java.util.List;
|
2025-10-24 17:52:11 +08:00
|
|
|
|
import java.util.Objects;
|
2025-11-12 16:42:51 +08:00
|
|
|
|
import java.util.Optional;
|
2025-10-20 10:16:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @author xf
|
|
|
|
|
|
*/
|
|
|
|
|
|
public class EaseChatAdapter extends BaseMultiItemQuickAdapter<EMMessageInfo, BaseViewHolder> {
|
|
|
|
|
|
|
|
|
|
|
|
// private int[] a = {R.drawable.random0s, R.drawable.random1s, R.drawable.random2s, R.drawable.random3s, R.drawable.random4s, R.drawable.random5s, R.drawable.random6s, R.drawable.random7s, R.drawable.random8s, R.drawable.random9s};
|
|
|
|
|
|
|
|
|
|
|
|
private int listShowType = 1;
|
|
|
|
|
|
public static int SHOW_TYPE_ALL = 1;
|
|
|
|
|
|
public static int SHOW_TYPE_USER = 2;
|
|
|
|
|
|
public static int SHOW_TYPE_SYSTEM = 3;
|
|
|
|
|
|
|
|
|
|
|
|
private List<EMMessageInfo> allMsgList;
|
|
|
|
|
|
private List<EMMessageInfo> userMsgList;
|
|
|
|
|
|
private List<EMMessageInfo> systemMsgList;
|
2025-10-24 17:52:11 +08:00
|
|
|
|
private String messageNameColor = "#00C8FF";//名称的颜色
|
|
|
|
|
|
private String messageGiftColor = "#FFE309";//礼物的颜色
|
2026-01-08 19:30:21 +08:00
|
|
|
|
public boolean isPriRoom = false;
|
2026-01-09 12:48:44 +08:00
|
|
|
|
public List<String> userIdList = new ArrayList<>();
|
2025-10-20 10:16:44 +08:00
|
|
|
|
|
|
|
|
|
|
public EaseChatAdapter() {
|
|
|
|
|
|
super(null);
|
|
|
|
|
|
addItemType(1, com.xscm.moduleutil.R.layout.ease_row_received_message_system);//系统消息、官方公告
|
|
|
|
|
|
addItemType(2, com.xscm.moduleutil.R.layout.ease_row_received_message_user_send);//用户发送的消息
|
2025-10-27 20:09:12 +08:00
|
|
|
|
addItemType(3, com.xscm.moduleutil.R.layout.ease_row_received_message_system);//加入房间 、赠送礼物、上下麦、禁言 2025年6月12日15:42:08,让和聊天显示的是同一个额布局,后面需要修改
|
2025-10-20 10:16:44 +08:00
|
|
|
|
addItemType(4, com.xscm.moduleutil.R.layout.ease_row_received_message_new_user);//新用户注册
|
|
|
|
|
|
addItemType(5, com.xscm.moduleutil.R.layout.ease_row_received_message_wagging);//摇签
|
|
|
|
|
|
addItemType(6, com.xscm.moduleutil.R.layout.ease_row_received_message_expression);//表情
|
|
|
|
|
|
// addItemType(8, com.qxcm.moduleutil.R.layout.ease_row_received_message_game);//球球大作战
|
|
|
|
|
|
// addItemType(9, com.qxcm.moduleutil.R.layout.ease_row_received_message_demand);//派单需求
|
|
|
|
|
|
// addItemType(10, com.qxcm.moduleutil.R.layout.ease_row_received_message_red_rain);//红包雨消息
|
|
|
|
|
|
// addItemType(11, com.qxcm.moduleutil.R.layout.ease_row_received_message_red_rain);//红包雨消息结束
|
|
|
|
|
|
|
|
|
|
|
|
allMsgList = new ArrayList<>();
|
|
|
|
|
|
userMsgList = new ArrayList<>();
|
|
|
|
|
|
systemMsgList = new ArrayList<>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setShowType(int type) {
|
|
|
|
|
|
if (type == listShowType) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
listShowType = type;
|
|
|
|
|
|
List currData = new ArrayList();
|
|
|
|
|
|
if (type == SHOW_TYPE_ALL) {
|
|
|
|
|
|
currData.addAll(allMsgList);
|
2025-10-24 17:52:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (type == SHOW_TYPE_USER) {
|
2025-10-20 10:16:44 +08:00
|
|
|
|
currData.addAll(userMsgList);
|
2025-10-24 17:52:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (type == SHOW_TYPE_SYSTEM) {
|
2025-10-20 10:16:44 +08:00
|
|
|
|
currData.addAll(systemMsgList);
|
|
|
|
|
|
}
|
|
|
|
|
|
setNewData(currData);
|
|
|
|
|
|
//notifyDataSetChanged();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-24 17:52:11 +08:00
|
|
|
|
private SpannableStringBuilder getSpannable(RoomMessageEvent message) {
|
|
|
|
|
|
|
|
|
|
|
|
// 在 Android 中实现类似的富文本功能
|
|
|
|
|
|
SpannableStringBuilder spannable = new SpannableStringBuilder(message.getText().getText() != null ? message.getText().getText() : "");
|
|
|
|
|
|
|
|
|
|
|
|
// 设置发送者昵称的颜色
|
|
|
|
|
|
if (message.getText().getFromUserInfo() != null && !TextUtils.isEmpty(message.getText().getFromUserInfo().getNickname())) {
|
|
|
|
|
|
String nickname = message.getText().getFromUserInfo().getNickname();
|
|
|
|
|
|
int startIndex = message.getText().getText().indexOf(nickname);
|
|
|
|
|
|
if (startIndex != -1) {
|
|
|
|
|
|
int endIndex = startIndex + nickname.length();
|
|
|
|
|
|
spannable.setSpan(new ForegroundColorSpan(Color.parseColor(messageNameColor)), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置接收者昵称的颜色
|
|
|
|
|
|
if (message.getText().getToUserInfo() != null && !TextUtils.isEmpty(message.getText().getToUserInfo().getNickname())) {
|
|
|
|
|
|
String nickname = message.getText().getToUserInfo().getNickname();
|
|
|
|
|
|
int startIndex = message.getText().getText().indexOf(nickname);
|
|
|
|
|
|
if (startIndex != -1) {
|
|
|
|
|
|
int endIndex = startIndex + nickname.length();
|
|
|
|
|
|
spannable.setSpan(new ForegroundColorSpan(Color.parseColor(messageNameColor)), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理礼物名称的颜色设置
|
|
|
|
|
|
if (message.getText().getGiftInfo() != null && !TextUtils.isEmpty(message.getText().getGiftInfo().getGift_name())) {
|
|
|
|
|
|
String[] giftNames = message.getText().getGiftInfo().getGift_name().split(",");
|
|
|
|
|
|
for (String giftName : giftNames) {
|
|
|
|
|
|
List<int[]> occurrences = findAllOccurrencesOfString(message.getText().getText(), giftName);
|
|
|
|
|
|
for (int[] range : occurrences) {
|
|
|
|
|
|
spannable.setSpan(new ForegroundColorSpan(Color.parseColor(messageGiftColor)), range[0], range[1], Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理多个礼物信息的颜色设置
|
|
|
|
|
|
if (message.getText().getGiftInfos() != null && !message.getText().getGiftInfos().isEmpty()) {
|
|
|
|
|
|
for (GiftBean gift : message.getText().getGiftInfos()) {
|
|
|
|
|
|
if (!TextUtils.isEmpty(gift.getGift_name())) {
|
|
|
|
|
|
int startIndex = message.getText().getText().indexOf(gift.getGift_name());
|
|
|
|
|
|
if (startIndex != -1) {
|
|
|
|
|
|
int endIndex = startIndex + gift.getGift_name().length();
|
|
|
|
|
|
spannable.setSpan(new ForegroundColorSpan(Color.parseColor(messageGiftColor)), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理多个接收者信息的颜色设置
|
|
|
|
|
|
if (message.getText().getToUserInfos() != null && !message.getText().getToUserInfos().isEmpty()) {
|
|
|
|
|
|
for (int i = 0; i < message.getText().getToUserInfos().size(); i++) {
|
|
|
|
|
|
UserInfo userModel = message.getText().getToUserInfos().get(i);
|
|
|
|
|
|
if (!TextUtils.isEmpty(userModel.getNickname())) {
|
|
|
|
|
|
List<int[]> occurrences = findAllOccurrencesOfString(message.getText().getText(), userModel.getNickname());
|
|
|
|
|
|
for (int[] range : occurrences) {
|
|
|
|
|
|
spannable.setSpan(new ForegroundColorSpan(Color.parseColor(messageNameColor)), range[0], range[1], Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return spannable;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 查找字符串中所有子字符串出现的位置
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param text 原始文本
|
|
|
|
|
|
* @param searchString 要查找的子字符串
|
|
|
|
|
|
* @return 包含所有出现位置的列表,每个位置用 [start, end] 表示
|
|
|
|
|
|
*/
|
|
|
|
|
|
private List<int[]> findAllOccurrencesOfString(String text, String searchString) {
|
|
|
|
|
|
List<int[]> occurrences = new ArrayList<>();
|
|
|
|
|
|
if (TextUtils.isEmpty(text) || TextUtils.isEmpty(searchString)) {
|
|
|
|
|
|
return occurrences;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int index = text.indexOf(searchString);
|
|
|
|
|
|
while (index >= 0) {
|
|
|
|
|
|
occurrences.add(new int[]{index, index + searchString.length()});
|
|
|
|
|
|
index = text.indexOf(searchString, index + searchString.length());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return occurrences;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-20 10:16:44 +08:00
|
|
|
|
@Override
|
|
|
|
|
|
protected void convert(BaseViewHolder helper, EMMessageInfo item) {
|
|
|
|
|
|
RoomMessageEvent emMessage = item.getEmMessage();
|
2025-10-25 18:07:21 +08:00
|
|
|
|
// 重置 ImageView 状态,防止复用导致的图片错乱
|
2025-10-24 17:52:11 +08:00
|
|
|
|
|
2025-10-20 10:16:44 +08:00
|
|
|
|
switch (helper.getItemViewType()) {
|
|
|
|
|
|
case 1:
|
2025-11-14 17:38:30 +08:00
|
|
|
|
helper.getView(com.xscm.moduleutil.R.id.tv_content).setVisibility(View.VISIBLE);
|
2026-01-09 09:38:42 +08:00
|
|
|
|
TextView tv1Content = helper.getView(com.xscm.moduleutil.R.id.tv_content);
|
|
|
|
|
|
if (isPriRoom){
|
2026-01-09 14:12:39 +08:00
|
|
|
|
if (emMessage.getRoomId().equals(AgoraManager.getInstance().getLastRoomId())){
|
2026-01-09 09:38:42 +08:00
|
|
|
|
tv1Content.setTextColor(Color.parseColor("#FF6B647E"));
|
|
|
|
|
|
helper.setText(com.xscm.moduleutil.R.id.tv_content, emMessage.getText().getText());
|
|
|
|
|
|
}else {
|
|
|
|
|
|
tv1Content.setTextColor(Color.parseColor("#FFFFFFFF"));
|
|
|
|
|
|
helper.setText(com.xscm.moduleutil.R.id.tv_content, getSpannable(emMessage));
|
|
|
|
|
|
}
|
|
|
|
|
|
}else {
|
|
|
|
|
|
tv1Content.setTextColor(Color.parseColor("#FFFFFFFF"));
|
|
|
|
|
|
helper.setText(com.xscm.moduleutil.R.id.tv_content, getSpannable(emMessage));
|
|
|
|
|
|
}
|
2025-10-24 17:52:11 +08:00
|
|
|
|
|
2025-10-20 10:16:44 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 2:
|
2025-10-25 18:07:21 +08:00
|
|
|
|
AdaptiveImageView emojiImageView = helper.getView(com.xscm.moduleutil.R.id.im_emj);
|
2025-12-02 19:26:37 +08:00
|
|
|
|
// 清除之前的图片加载任务,防止图片显示在错误的位置
|
2025-10-25 18:07:21 +08:00
|
|
|
|
Glide.with(mContext).clear(emojiImageView);
|
2025-12-02 19:26:37 +08:00
|
|
|
|
emojiImageView.setTag(null);
|
2025-10-25 18:07:21 +08:00
|
|
|
|
emojiImageView.setVisibility(View.VISIBLE);
|
2025-10-24 17:52:11 +08:00
|
|
|
|
helper.getView(com.xscm.moduleutil.R.id.tv_content).setVisibility(View.GONE);
|
|
|
|
|
|
|
2025-11-12 16:42:51 +08:00
|
|
|
|
MeHeadView avatar = helper.getView(com.xscm.moduleutil.R.id.avatar);
|
|
|
|
|
|
avatar.setData(emMessage.getText().getFromUserInfo().getAvatar(), "", emMessage.getText().getFromUserInfo().getNobility_image());
|
2025-11-12 19:09:21 +08:00
|
|
|
|
// ImageUtils.loadHeadCC(emMessage.getText().getFromUserInfo().getAvatar(), helper.getView(com.xscm.moduleutil.R.id.avatar));
|
2025-11-14 17:38:30 +08:00
|
|
|
|
ShineTextView tvName = helper.getView(com.xscm.moduleutil.R.id.tv_name);
|
2025-12-02 19:26:37 +08:00
|
|
|
|
// 先重置tvName状态,防止复用时出现混乱
|
|
|
|
|
|
tvName.setShine(false);
|
|
|
|
|
|
tvName.clearAnimation();
|
2025-11-14 17:38:30 +08:00
|
|
|
|
tvName.setText(emMessage.getText().getFromUserInfo().getNickname());
|
|
|
|
|
|
|
2026-01-17 01:08:24 +08:00
|
|
|
|
if (!emMessage.getText().getFromUserInfo().getNickname_color().isEmpty()) {
|
2026-01-17 00:32:39 +08:00
|
|
|
|
emMessage.getText().getFromUserInfo().setNickname_color("#ff5878");
|
2025-11-14 17:38:30 +08:00
|
|
|
|
tvName.setStartColor(Color.parseColor(emMessage.getText().getFromUserInfo().getNickname_color()));
|
|
|
|
|
|
tvName.setShineColor(Color.parseColor(emMessage.getText().getFromUserInfo().getNickname_color()));
|
|
|
|
|
|
tvName.setEndColor(Color.parseColor(emMessage.getText().getFromUserInfo().getNickname_color()));
|
|
|
|
|
|
tvName.setShine(true);
|
|
|
|
|
|
tvName.setShineType(0);
|
2025-12-11 21:04:25 +08:00
|
|
|
|
} else {
|
2025-12-02 19:26:37 +08:00
|
|
|
|
tvName.setStartColor(Color.parseColor("#FFFFFFFF"));
|
|
|
|
|
|
tvName.setShineColor(Color.parseColor("#FFFFFFFF"));
|
|
|
|
|
|
tvName.setEndColor(Color.parseColor("#FFFFFFFF"));
|
|
|
|
|
|
tvName.setShine(true);
|
|
|
|
|
|
tvName.setShineType(1);
|
2025-11-14 17:38:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// helper.setTextColor(com.xscm.moduleutil.R.id.tv_name, emMessage.getText().getFromUserInfo().getNickname_color() != null && !emMessage.getText().getFromUserInfo().getNickname_color().isEmpty() ? Color.parseColor(emMessage.getText().getFromUserInfo().getNickname_color()) : Color.parseColor("#CCA882"));
|
2025-10-24 17:52:11 +08:00
|
|
|
|
if (emMessage.getText().getText() != null) {
|
|
|
|
|
|
helper.setText(com.xscm.moduleutil.R.id.tv_content, getSpannable(emMessage));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// helper.setText(com.xscm.moduleutil.R.id.tv_content, emMessage.getText().getText());
|
2025-10-20 10:16:44 +08:00
|
|
|
|
List<String> images = emMessage.getText().getFromUserInfo().getIcon();
|
|
|
|
|
|
LinearLayout ll_images = helper.getView(com.xscm.moduleutil.R.id.line);
|
|
|
|
|
|
ll_images.removeAllViews();
|
|
|
|
|
|
for (String url : images) {
|
2025-12-11 21:04:25 +08:00
|
|
|
|
if (url != null && url.contains("http")) {
|
2025-10-20 10:16:44 +08:00
|
|
|
|
ImageView imageView = new ImageView(helper.itemView.getContext());
|
|
|
|
|
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
|
2025-10-25 18:07:21 +08:00
|
|
|
|
helper.itemView.getContext().getResources().getDimensionPixelSize(com.xscm.moduleutil.R.dimen.dp_57),
|
|
|
|
|
|
helper.itemView.getContext().getResources().getDimensionPixelSize(com.xscm.moduleutil.R.dimen.dp_15)
|
2025-10-20 10:16:44 +08:00
|
|
|
|
);
|
|
|
|
|
|
params.setMargins(0, 0, helper.itemView.getContext().getResources().getDimensionPixelSize(com.xscm.moduleutil.R.dimen.dp_5), 0); // 右边距
|
|
|
|
|
|
imageView.setLayoutParams(params);
|
|
|
|
|
|
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
|
|
|
|
|
|
// 使用 Glide 加载图片
|
|
|
|
|
|
ImageUtils.loadHeadCC(url, imageView);
|
|
|
|
|
|
ll_images.addView(imageView);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 19:26:37 +08:00
|
|
|
|
// 为聊天气泡设置唯一标识,防止异步加载后应用到错误的视图
|
|
|
|
|
|
String bubbleId = emMessage.getRoomId() + "_" + helper.getLayoutPosition() + "_bubble";
|
|
|
|
|
|
View bubbleView = helper.getView(com.xscm.moduleutil.R.id.bubble);
|
|
|
|
|
|
bubbleView.setTag(bubbleId);
|
2025-12-11 21:04:25 +08:00
|
|
|
|
|
2025-10-24 17:52:11 +08:00
|
|
|
|
if (emMessage.getText().getFromUserInfo().getChat_bubble() != null && !emMessage.getText().getFromUserInfo().getChat_bubble().isEmpty()) {
|
|
|
|
|
|
// 使用 Glide 加载图片并设置名称
|
|
|
|
|
|
String imageUrl = emMessage.getText().getFromUserInfo().getChat_bubble();
|
|
|
|
|
|
Glide.with(mContext)
|
|
|
|
|
|
.load(imageUrl)
|
|
|
|
|
|
.into(new SimpleTarget<Drawable>() {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
|
2025-12-02 19:26:37 +08:00
|
|
|
|
// 检查视图是否仍属于当前数据项
|
|
|
|
|
|
if (bubbleId.equals(bubbleView.getTag())) {
|
|
|
|
|
|
// 将资源转换为 Bitmap
|
|
|
|
|
|
if (resource instanceof BitmapDrawable) {
|
|
|
|
|
|
Bitmap bitmap = ((BitmapDrawable) resource).getBitmap();
|
|
|
|
|
|
// 创建新的文件名
|
|
|
|
|
|
Drawable fakeNinePatch = new FakeNinePatchDrawable(bitmap, 0.4f, 0.4f, 0.6f, 0.5f);
|
|
|
|
|
|
|
|
|
|
|
|
// 设置背景
|
|
|
|
|
|
bubbleView.setBackground(fakeNinePatch);
|
|
|
|
|
|
}
|
2025-10-24 17:52:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void onLoadFailed(@Nullable Drawable errorDrawable) {
|
2025-12-02 19:26:37 +08:00
|
|
|
|
// 检查视图是否仍属于当前数据项
|
|
|
|
|
|
if (bubbleId.equals(bubbleView.getTag())) {
|
|
|
|
|
|
// 加载失败时使用默认背景
|
|
|
|
|
|
bubbleView.setBackgroundResource(com.xscm.moduleutil.R.drawable.ease_row_pubilc_user_bg);
|
|
|
|
|
|
}
|
2025-10-24 17:52:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
2025-12-02 19:26:37 +08:00
|
|
|
|
// 检查视图是否仍属于当前数据项
|
|
|
|
|
|
if (bubbleId.equals(bubbleView.getTag())) {
|
|
|
|
|
|
bubbleView.setBackgroundResource(com.xscm.moduleutil.R.drawable.ease_row_pubilc_user_bg);
|
|
|
|
|
|
}
|
2025-10-24 17:52:11 +08:00
|
|
|
|
}
|
2025-11-12 19:09:21 +08:00
|
|
|
|
if (emMessage.getText().getEmoji() != null && emMessage.getText().getEmoji().getAnimate_image() != null) {
|
2025-10-25 18:07:21 +08:00
|
|
|
|
|
|
|
|
|
|
// 为每个加载任务设置唯一标识
|
2025-12-02 19:26:37 +08:00
|
|
|
|
String uniqueId = emMessage.getRoomId() + "_" + helper.getLayoutPosition() + "_emoji";
|
2025-10-25 18:07:21 +08:00
|
|
|
|
emojiImageView.setTag(uniqueId);
|
|
|
|
|
|
|
2025-11-12 16:42:51 +08:00
|
|
|
|
//这段代码使用 Optional 链式调用安全地获取 emMessage 中的 animate_image 字段,避免空指针异常。如果任意环节为 null,则返回空字符串。
|
|
|
|
|
|
String animateImage = Optional.ofNullable(emMessage)
|
|
|
|
|
|
.map(RoomMessageEvent::getText)
|
|
|
|
|
|
.map(RoomMessageEvent.T::getEmoji)
|
|
|
|
|
|
.map(EmotionDeatils::getAnimate_image)
|
|
|
|
|
|
.orElse("");
|
2025-10-25 18:07:21 +08:00
|
|
|
|
|
2025-12-05 20:26:05 +08:00
|
|
|
|
// 如果有动画图片URL,加载动画
|
2025-11-12 19:09:21 +08:00
|
|
|
|
if (!animateImage.isEmpty()) {
|
2025-10-25 18:07:21 +08:00
|
|
|
|
// 检查视图是否仍属于当前数据项
|
|
|
|
|
|
if (uniqueId.equals(emojiImageView.getTag())) {
|
2025-12-05 20:26:05 +08:00
|
|
|
|
// 使用 RequestListener 监听加载完成
|
|
|
|
|
|
Glide.with(mContext)
|
|
|
|
|
|
.load(animateImage)
|
|
|
|
|
|
.listener(new RequestListener<Drawable>() {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target,
|
|
|
|
|
|
DataSource dataSource, boolean isFirstResource) {
|
|
|
|
|
|
// 检查视图是否仍属于当前数据项
|
|
|
|
|
|
if (uniqueId.equals(emojiImageView.getTag())) {
|
|
|
|
|
|
// 加载完成后执行
|
|
|
|
|
|
new Handler().postDelayed(() -> {
|
|
|
|
|
|
// 再次检查视图状态
|
|
|
|
|
|
if (uniqueId.equals(emojiImageView.getTag())) {
|
2025-12-29 22:29:01 +08:00
|
|
|
|
try {
|
2026-01-05 15:43:25 +08:00
|
|
|
|
Glide.with(emojiImageView)
|
2025-12-29 22:29:01 +08:00
|
|
|
|
.load(emMessage.getText().getEmoji().getImage())
|
|
|
|
|
|
.into(emojiImageView);
|
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
|
LogUtils.e("e:"+e);
|
|
|
|
|
|
}
|
2025-12-05 20:26:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
}, 1500);
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target,
|
|
|
|
|
|
boolean isFirstResource) {
|
|
|
|
|
|
// 加载失败时检查视图是否仍属于当前数据项
|
|
|
|
|
|
if (uniqueId.equals(emojiImageView.getTag())) {
|
|
|
|
|
|
// 加载静态表情作为备选
|
|
|
|
|
|
Glide.with(mContext)
|
|
|
|
|
|
.load(emMessage.getText().getEmoji().getImage())
|
|
|
|
|
|
.into(emojiImageView);
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
.into(emojiImageView);
|
2025-10-25 18:07:21 +08:00
|
|
|
|
}
|
2025-10-24 17:52:11 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-05 20:26:05 +08:00
|
|
|
|
// 如果没有动画图片,直接加载静态图片
|
|
|
|
|
|
// 检查视图是否仍属于当前数据项
|
|
|
|
|
|
if (uniqueId.equals(emojiImageView.getTag())) {
|
2025-11-12 16:42:51 +08:00
|
|
|
|
Glide.with(mContext)
|
2025-12-05 20:26:05 +08:00
|
|
|
|
.load(emMessage.getText().getEmoji().getImage())
|
2025-11-12 16:42:51 +08:00
|
|
|
|
.into(emojiImageView);
|
|
|
|
|
|
}
|
2025-10-24 17:52:11 +08:00
|
|
|
|
} else {
|
2025-12-02 19:26:37 +08:00
|
|
|
|
// 确保在非表情情况下,重置视图状态
|
2025-10-24 17:52:11 +08:00
|
|
|
|
helper.getView(com.xscm.moduleutil.R.id.tv_content).setVisibility(View.VISIBLE);
|
2025-10-25 18:07:21 +08:00
|
|
|
|
emojiImageView.setVisibility(View.GONE);
|
2025-12-02 19:26:37 +08:00
|
|
|
|
// 清除可能存在的异步加载任务
|
|
|
|
|
|
Glide.with(mContext).clear(emojiImageView);
|
2025-10-24 17:52:11 +08:00
|
|
|
|
}
|
2026-01-09 09:38:42 +08:00
|
|
|
|
|
|
|
|
|
|
TextView tvContent = helper.getView(com.xscm.moduleutil.R.id.tv_content);
|
|
|
|
|
|
ShineTextView tvContentName = helper.getView(com.xscm.moduleutil.R.id.tv_name);
|
|
|
|
|
|
if (isPriRoom){
|
2026-01-09 12:48:44 +08:00
|
|
|
|
if (emMessage.getRoomId().equals(AgoraManager.getInstance().getLastRoomId()) && !userIdList.contains(emMessage.getText().getFromUserInfo().getUser_id()+"")){
|
2026-01-09 09:38:42 +08:00
|
|
|
|
tvContent.setTextColor(Color.parseColor("#FF6B647E"));
|
2026-01-09 12:48:44 +08:00
|
|
|
|
setTvNameColor(tvContentName,"#FF6B647E");
|
2026-01-09 09:38:42 +08:00
|
|
|
|
}else {
|
|
|
|
|
|
tvContent.setTextColor(Color.parseColor("#FFFFFF"));
|
|
|
|
|
|
setTvNameColor(tvContentName,"#FFFFFF");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-24 17:52:11 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case 3:
|
2025-11-14 17:38:30 +08:00
|
|
|
|
helper.getView(com.xscm.moduleutil.R.id.tv_content).setVisibility(View.VISIBLE);
|
2026-01-09 09:38:42 +08:00
|
|
|
|
TextView tv3Content = helper.getView(com.xscm.moduleutil.R.id.tv_content);
|
|
|
|
|
|
if (isPriRoom){
|
2026-01-09 12:48:44 +08:00
|
|
|
|
if (emMessage.getRoomId().equals(AgoraManager.getInstance().getLastRoomId()) && !userIdList.contains(emMessage.getText().getFromUserInfo().getUser_id()+"")){
|
2026-01-09 09:38:42 +08:00
|
|
|
|
tv3Content.setTextColor(Color.parseColor("#FF6B647E"));
|
|
|
|
|
|
helper.setText(com.xscm.moduleutil.R.id.tv_content, emMessage.getText().getText());
|
|
|
|
|
|
}else {
|
|
|
|
|
|
tv3Content.setTextColor(Color.parseColor("#FFFFFFFF"));
|
|
|
|
|
|
helper.setText(com.xscm.moduleutil.R.id.tv_content, getSpannable(emMessage));
|
|
|
|
|
|
}
|
|
|
|
|
|
}else {
|
|
|
|
|
|
tv3Content.setTextColor(Color.parseColor("#FFFFFFFF"));
|
|
|
|
|
|
helper.setText(com.xscm.moduleutil.R.id.tv_content, getSpannable(emMessage));
|
|
|
|
|
|
}
|
2025-10-20 10:16:44 +08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
2026-01-09 09:38:42 +08:00
|
|
|
|
}
|
2025-10-20 10:16:44 +08:00
|
|
|
|
|
2026-01-09 09:38:42 +08:00
|
|
|
|
private void setTvNameColor(ShineTextView tvName,String color){
|
|
|
|
|
|
tvName.setStartColor(Color.parseColor(color));
|
|
|
|
|
|
tvName.setShineColor(Color.parseColor(color));
|
|
|
|
|
|
tvName.setEndColor(Color.parseColor(color));
|
|
|
|
|
|
tvName.setShine(true);
|
|
|
|
|
|
tvName.setShineType(1);
|
2025-10-20 10:16:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-24 17:52:11 +08:00
|
|
|
|
|
|
|
|
|
|
private void loadBubbleBackground(Context context, String bubbleUrl, ConstraintLayout messageTextView) {
|
|
|
|
|
|
// String bubbleUrl = "https://midi01.oss-cn-beijing.aliyuncs.com/iosaixinqipao_17610381079117.png";
|
|
|
|
|
|
|
|
|
|
|
|
BubbleBackgroundHelper.loadBubbleBackground(context, bubbleUrl,
|
|
|
|
|
|
new BubbleBackgroundHelper.OnBubbleBackgroundLoadedListener() {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void onBackgroundLoaded(Drawable bubbleDrawable) {
|
|
|
|
|
|
// 设置背景
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
|
|
|
|
|
|
messageTextView.setBackground(bubbleDrawable);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
messageTextView.setBackgroundDrawable(bubbleDrawable);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 设置内边距,让文字不会贴边
|
|
|
|
|
|
int padding = dpToPx(context, 10);
|
|
|
|
|
|
messageTextView.setPadding(padding, padding, padding, padding);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void onLoadFailed() {
|
|
|
|
|
|
// 加载失败,使用默认背景
|
|
|
|
|
|
messageTextView.setBackgroundResource(com.xscm.moduleutil.R.drawable.ease_row_pubilc_user_bg);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private int dpToPx(Context context, int dp) {
|
|
|
|
|
|
return (int) (dp * context.getResources().getDisplayMetrics().density);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-20 10:16:44 +08:00
|
|
|
|
@Override
|
|
|
|
|
|
public void onViewRecycled(@NonNull BaseViewHolder holder) {
|
2025-11-14 17:38:30 +08:00
|
|
|
|
LogUtils.e("onViewRecycled", "onViewRecycled");
|
2025-10-25 18:07:21 +08:00
|
|
|
|
// AdaptiveImageView imEmj = (AdaptiveImageView)holder.getView(com.xscm.moduleutil.R.id.im_emj);
|
2025-11-12 16:42:51 +08:00
|
|
|
|
MeHeadView avatarOvalView = holder.getView(com.xscm.moduleutil.R.id.avatar);
|
2025-10-20 10:16:44 +08:00
|
|
|
|
ImageView ivNobility = holder.getView(com.xscm.moduleutil.R.id.iv_nobility);
|
|
|
|
|
|
ImageView ivCharm = holder.getView(com.xscm.moduleutil.R.id.iv_charm);
|
2025-10-25 18:07:21 +08:00
|
|
|
|
|
2025-10-20 10:16:44 +08:00
|
|
|
|
if (mContext instanceof Activity && (((Activity) mContext).isFinishing() || ((Activity) mContext).isDestroyed())) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-10-25 18:07:21 +08:00
|
|
|
|
|
2025-10-20 10:16:44 +08:00
|
|
|
|
try {
|
2025-10-25 18:07:21 +08:00
|
|
|
|
// 清理表情图片
|
|
|
|
|
|
if (holder.getView(com.xscm.moduleutil.R.id.im_emj) != null) {
|
|
|
|
|
|
ImageView imEmj = (ImageView) holder.getView(com.xscm.moduleutil.R.id.im_emj);
|
|
|
|
|
|
imEmj.setTag(null);
|
|
|
|
|
|
imEmj.setImageDrawable(null);
|
|
|
|
|
|
Glide.with(imEmj.getContext()).clear(imEmj);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 清理其他图片
|
2025-10-24 17:52:11 +08:00
|
|
|
|
if (avatarOvalView != null) {
|
|
|
|
|
|
Glide.with(avatarOvalView.getContext()).clear(avatarOvalView);
|
|
|
|
|
|
}
|
2025-10-20 10:16:44 +08:00
|
|
|
|
if (ivNobility != null) {
|
|
|
|
|
|
ivNobility.setImageDrawable(null);
|
|
|
|
|
|
Glide.with(ivNobility.getContext()).clear(ivNobility);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (ivCharm != null) {
|
|
|
|
|
|
ivCharm.setImageDrawable(null);
|
|
|
|
|
|
Glide.with(ivCharm.getContext()).clear(ivCharm);
|
|
|
|
|
|
}
|
2025-12-02 19:26:37 +08:00
|
|
|
|
|
|
|
|
|
|
// 重置tvName的闪动状态,防止复用时出现混乱
|
|
|
|
|
|
ShineTextView tvName = holder.getView(com.xscm.moduleutil.R.id.tv_name);
|
|
|
|
|
|
if (tvName != null) {
|
|
|
|
|
|
tvName.setShine(false);
|
|
|
|
|
|
tvName.clearAnimation();
|
|
|
|
|
|
}
|
2025-10-20 10:16:44 +08:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
super.onViewRecycled(holder);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-25 18:07:21 +08:00
|
|
|
|
|
2025-10-20 10:16:44 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 清除数据
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param
|
|
|
|
|
|
*/
|
|
|
|
|
|
public void clearData() {
|
|
|
|
|
|
this.mData.clear();
|
|
|
|
|
|
userMsgList.clear();
|
|
|
|
|
|
systemMsgList.clear();
|
|
|
|
|
|
allMsgList.clear();
|
2025-12-05 16:41:32 +08:00
|
|
|
|
getData().clear();
|
2025-10-20 10:16:44 +08:00
|
|
|
|
notifyDataSetChanged();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void clearSomeData() {
|
|
|
|
|
|
if (this.mData != null && mData.size() > 500) {
|
|
|
|
|
|
List<EMMessageInfo> emMessageInfos = mData.subList(500, mData.size());
|
|
|
|
|
|
setNewData(emMessageInfos);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (allMsgList.size() > 500) {
|
|
|
|
|
|
allMsgList = allMsgList.subList(500, allMsgList.size());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (userMsgList.size() > 500) {
|
|
|
|
|
|
userMsgList = userMsgList.subList(500, userMsgList.size());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (systemMsgList.size() > 500) {
|
|
|
|
|
|
systemMsgList = systemMsgList.subList(500, systemMsgList.size());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void addData(@NonNull EMMessageInfo data) {
|
|
|
|
|
|
|
2025-10-24 17:52:11 +08:00
|
|
|
|
// 添加到对应的存储列表
|
|
|
|
|
|
addToStorageLists(data);
|
|
|
|
|
|
|
|
|
|
|
|
// 维护数据量限制
|
|
|
|
|
|
maintainDataLimits();
|
|
|
|
|
|
|
|
|
|
|
|
// 根据显示类型决定是否添加到适配器
|
|
|
|
|
|
if (shouldAddToAdapter(data)) {
|
2025-10-20 10:16:44 +08:00
|
|
|
|
super.addData(data);
|
2025-10-24 17:52:11 +08:00
|
|
|
|
// 确保适配器数据量也不超过限制
|
2026-01-13 19:59:17 +08:00
|
|
|
|
if (mData.size() > 200) {
|
|
|
|
|
|
int removeCount = mData.size() - 200;
|
|
|
|
|
|
mData.subList(0, 100).clear();
|
2025-10-24 17:52:11 +08:00
|
|
|
|
notifyDataSetChanged();
|
|
|
|
|
|
}
|
2025-10-20 10:16:44 +08:00
|
|
|
|
}
|
2025-10-24 17:52:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void addToStorageLists(EMMessageInfo data) {
|
|
|
|
|
|
switch (data.getItemType()) {
|
|
|
|
|
|
case 1: // 系统消息
|
|
|
|
|
|
allMsgList.add(data);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 2: // 用户消息
|
|
|
|
|
|
userMsgList.add(data);
|
|
|
|
|
|
allMsgList.add(data);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 3: // 系统消息类型3
|
|
|
|
|
|
systemMsgList.add(data);
|
|
|
|
|
|
allMsgList.add(data);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void maintainDataLimits() {
|
|
|
|
|
|
// 确保所有存储列表都不超过最大限制
|
|
|
|
|
|
trimListToMaxSize(allMsgList);
|
|
|
|
|
|
trimListToMaxSize(userMsgList);
|
|
|
|
|
|
trimListToMaxSize(systemMsgList);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void trimListToMaxSize(List<EMMessageInfo> list) {
|
2026-01-13 19:59:17 +08:00
|
|
|
|
if (list.size() > 200) {
|
|
|
|
|
|
int removeCount = list.size() - 200;
|
|
|
|
|
|
list.subList(0, 100).clear();
|
2025-10-24 17:52:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private boolean shouldAddToAdapter(EMMessageInfo data) {
|
|
|
|
|
|
// return (listShowType == SHOW_TYPE_ALL && (data.getItemType() == 1 || data.getItemType() == 2)) ||
|
|
|
|
|
|
// (listShowType == SHOW_TYPE_USER && data.getItemType() == 2) ||
|
|
|
|
|
|
// (listShowType == SHOW_TYPE_SYSTEM && data.getItemType() == 3);
|
|
|
|
|
|
return true;
|
2025-10-20 10:16:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void addData(@NonNull Collection<? extends EMMessageInfo> newData) {
|
|
|
|
|
|
// super.addData(newData);
|
|
|
|
|
|
if (newData == null || newData.size() == 0) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
List<EMMessageInfo> list = new ArrayList<>();
|
|
|
|
|
|
list.addAll(newData);
|
|
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
|
|
addData(list.get(i));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|