refactor: 重构conversationAdapter

使用ListAdapter重构conversationAdapter以提高效率
This commit is contained in:
糕小菜 2024-11-29 10:07:03 +08:00
parent a616124534
commit 0d56f17f37

View File

@ -3,10 +3,11 @@ package com.kaixed.kchat.ui.adapter
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
@ -16,7 +17,7 @@ import com.kaixed.kchat.R
import com.kaixed.kchat.data.local.entity.Conversation
import com.kaixed.kchat.databinding.ChatMainItemBinding
import com.kaixed.kchat.ui.activity.ChatActivity
import com.kaixed.kchat.ui.i.OnChatListItemClickListener
import com.kaixed.kchat.ui.i.OnItemListener
import com.kaixed.kchat.utils.TextUtil
/**
@ -24,32 +25,10 @@ import com.kaixed.kchat.utils.TextUtil
* @Date: 2024/10/27 18:22
*/
class ConversationAdapter(
private var chatLists: List<Conversation>, private val context: Context
) : RecyclerView.Adapter<ConversationAdapter.MyViewHolder?>() {
private val context: Context
) : ListAdapter<Conversation, ConversationAdapter.MyViewHolder>(ConversationDiffCallback()) {
private var onChatListItemClickListener: OnChatListItemClickListener? = null
companion object {
private const val TAG = "ConversationAdapter"
}
fun updateData(newChatLists: List<Conversation>) {
chatLists = newChatLists
}
class MyViewHolder(val binding: ChatMainItemBinding) : RecyclerView.ViewHolder(binding.root) {
fun bindData(chatList: Conversation, context: Context) {
binding.tvContent.text = chatList.lastContent.replaceSpan("[委屈]") {
CenterImageSpan(context, R.drawable.emoji).setDrawableSize(55)
}
binding.tvNickname.text = chatList.talkerId
binding.tvTimestamp.text = TextUtil.getTimestampString(chatList.timestamp)
}
}
fun setItemListener(i: OnChatListItemClickListener?) {
this.onChatListItemClickListener = i
}
private var onItemListener: OnItemListener? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val binding = ChatMainItemBinding.inflate(
@ -59,14 +38,14 @@ class ConversationAdapter(
}
override fun onBindViewHolder(
holder: MyViewHolder, @SuppressLint("RecyclerView") position: Int
holder: MyViewHolder,
@SuppressLint("RecyclerView") position: Int
) {
Log.d(TAG, "onBindViewHolder: $chatLists")
val options = RequestOptions()
.placeholder(R.drawable.ic_default_avatar)
.error(R.drawable.ic_default_avatar)
val chatList = chatLists[position]
val chatList = getItem(position)
if (chatList.avatarUrl.isNotEmpty()) {
Glide.with(context).load(chatList.avatarUrl).apply(options)
.into(holder.binding.ifvAvatar)
@ -79,17 +58,44 @@ class ConversationAdapter(
holder.binding.rlUnreadCount.visibility = View.INVISIBLE
}
holder.bindData(chatLists[position], context)
holder.bind(chatList, context)
holder.itemView.setOnClickListener {
onChatListItemClickListener!!.onItemClick(chatLists[position].talkerId)
onItemListener!!.onItemClick(chatList.talkerId)
context.startActivity(Intent(context, ChatActivity::class.java).apply {
putExtra("contactId", chatLists[position].talkerId)
putExtra("contactNickname", chatLists[position].nickname)
putExtra("contactId", chatList.talkerId)
putExtra("contactNickname", chatList.nickname)
})
}
holder.itemView.setOnLongClickListener {
onItemListener!!.onItemLongClick(chatList.talkerId, chatList.nickname)
true
}
}
override fun getItemCount(): Int = chatLists.size
inner class MyViewHolder(val binding: ChatMainItemBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(chatList: Conversation, context: Context) {
binding.tvContent.text = chatList.lastContent.replaceSpan("[委屈]") {
CenterImageSpan(context, R.drawable.emoji).setDrawableSize(55)
}
binding.tvNickname.text = chatList.talkerId
binding.tvTimestamp.text = TextUtil.getTimestampString(chatList.timestamp)
}
}
fun setItemListener(i: OnItemListener?) {
this.onItemListener = i
}
class ConversationDiffCallback : DiffUtil.ItemCallback<Conversation>() {
override fun areItemsTheSame(oldItem: Conversation, newItem: Conversation): Boolean {
return oldItem.talkerId == newItem.talkerId
}
override fun areContentsTheSame(oldItem: Conversation, newItem: Conversation): Boolean {
return oldItem == newItem
}
}
}