2025-12-18 14:20:41 +08:00
|
|
|
|
package com.example.livestreaming;
|
|
|
|
|
|
|
2025-12-22 16:31:46 +08:00
|
|
|
|
import android.graphics.Outline;
|
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
|
import android.text.TextUtils;
|
2025-12-18 14:20:41 +08:00
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
|
import android.view.View;
|
|
|
|
|
|
import android.view.ViewGroup;
|
2025-12-22 16:31:46 +08:00
|
|
|
|
import android.view.ViewOutlineProvider;
|
2025-12-18 14:20:41 +08:00
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
|
import androidx.recyclerview.widget.DiffUtil;
|
|
|
|
|
|
import androidx.recyclerview.widget.ListAdapter;
|
|
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
|
|
2025-12-22 16:31:46 +08:00
|
|
|
|
import com.bumptech.glide.Glide;
|
2025-12-18 14:20:41 +08:00
|
|
|
|
import com.example.livestreaming.databinding.ItemConversationBinding;
|
|
|
|
|
|
|
|
|
|
|
|
public class ConversationsAdapter extends ListAdapter<ConversationItem, ConversationsAdapter.VH> {
|
|
|
|
|
|
|
|
|
|
|
|
public interface OnConversationClickListener {
|
|
|
|
|
|
void onConversationClick(ConversationItem item);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private final OnConversationClickListener onConversationClickListener;
|
|
|
|
|
|
|
|
|
|
|
|
public ConversationsAdapter(OnConversationClickListener onConversationClickListener) {
|
|
|
|
|
|
super(DIFF);
|
|
|
|
|
|
this.onConversationClickListener = onConversationClickListener;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
|
|
ItemConversationBinding binding = ItemConversationBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
|
|
|
|
|
|
return new VH(binding, onConversationClickListener);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void onBindViewHolder(@NonNull VH holder, int position) {
|
|
|
|
|
|
holder.bind(getItem(position));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static class VH extends RecyclerView.ViewHolder {
|
|
|
|
|
|
|
|
|
|
|
|
private final ItemConversationBinding binding;
|
|
|
|
|
|
private final OnConversationClickListener onConversationClickListener;
|
|
|
|
|
|
|
|
|
|
|
|
VH(ItemConversationBinding binding, OnConversationClickListener onConversationClickListener) {
|
|
|
|
|
|
super(binding.getRoot());
|
|
|
|
|
|
this.binding = binding;
|
|
|
|
|
|
this.onConversationClickListener = onConversationClickListener;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void bind(ConversationItem item) {
|
|
|
|
|
|
binding.title.setText(item != null && item.getTitle() != null ? item.getTitle() : "");
|
|
|
|
|
|
binding.lastMessage.setText(item != null && item.getLastMessage() != null ? item.getLastMessage() : "");
|
|
|
|
|
|
binding.timeText.setText(item != null && item.getTimeText() != null ? item.getTimeText() : "");
|
|
|
|
|
|
|
|
|
|
|
|
int unread = item != null ? item.getUnreadCount() : 0;
|
|
|
|
|
|
if (unread > 0) {
|
|
|
|
|
|
binding.unreadBadge.setVisibility(View.VISIBLE);
|
|
|
|
|
|
binding.unreadBadge.setText(unread > 99 ? "99+" : String.valueOf(unread));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
binding.unreadBadge.setVisibility(View.GONE);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (item != null && item.isMuted()) {
|
|
|
|
|
|
binding.muteIcon.setVisibility(View.VISIBLE);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
binding.muteIcon.setVisibility(View.GONE);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-22 16:31:46 +08:00
|
|
|
|
// 加载头像
|
|
|
|
|
|
loadAvatar(item);
|
|
|
|
|
|
|
2025-12-18 14:20:41 +08:00
|
|
|
|
binding.getRoot().setOnClickListener(v -> {
|
|
|
|
|
|
if (item == null) return;
|
|
|
|
|
|
if (onConversationClickListener != null) onConversationClickListener.onConversationClick(item);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-12-22 16:31:46 +08:00
|
|
|
|
|
|
|
|
|
|
private void loadAvatar(ConversationItem item) {
|
|
|
|
|
|
if (binding.avatar == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
// 设置圆形裁剪
|
|
|
|
|
|
setupAvatarOutline();
|
|
|
|
|
|
|
|
|
|
|
|
// 根据会话ID或标题加载对应的头像
|
|
|
|
|
|
// 这里可以根据实际需求从服务器或本地加载
|
|
|
|
|
|
// 暂时使用默认头像,或者根据会话类型加载不同头像
|
|
|
|
|
|
try {
|
|
|
|
|
|
String conversationId = item != null ? item.getId() : null;
|
|
|
|
|
|
|
|
|
|
|
|
// 可以根据conversationId从SharedPreferences或其他地方加载头像
|
|
|
|
|
|
// 这里先使用默认头像,使用Glide确保圆形裁剪
|
|
|
|
|
|
Glide.with(binding.avatar)
|
|
|
|
|
|
.load(R.drawable.ic_account_circle_24)
|
|
|
|
|
|
.circleCrop()
|
|
|
|
|
|
.into(binding.avatar);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
Glide.with(binding.avatar)
|
|
|
|
|
|
.load(R.drawable.ic_account_circle_24)
|
|
|
|
|
|
.circleCrop()
|
|
|
|
|
|
.into(binding.avatar);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void setupAvatarOutline() {
|
|
|
|
|
|
if (binding.avatar == null) return;
|
|
|
|
|
|
// 设置圆形裁剪,确保在模拟器上也能正常工作
|
|
|
|
|
|
binding.avatar.post(() -> {
|
|
|
|
|
|
if (binding.avatar.getWidth() > 0 && binding.avatar.getHeight() > 0) {
|
|
|
|
|
|
binding.avatar.setOutlineProvider(new ViewOutlineProvider() {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void getOutline(View view, Outline outline) {
|
|
|
|
|
|
outline.setOval(0, 0, view.getWidth(), view.getHeight());
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
binding.avatar.setClipToOutline(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2025-12-18 14:20:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static final DiffUtil.ItemCallback<ConversationItem> DIFF = new DiffUtil.ItemCallback<ConversationItem>() {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public boolean areItemsTheSame(@NonNull ConversationItem oldItem, @NonNull ConversationItem newItem) {
|
|
|
|
|
|
String o = oldItem.getId();
|
|
|
|
|
|
String n = newItem.getId();
|
|
|
|
|
|
return o != null && o.equals(n);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public boolean areContentsTheSame(@NonNull ConversationItem oldItem, @NonNull ConversationItem newItem) {
|
|
|
|
|
|
return oldItem.equals(newItem);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|