From 0d56f17f3796edeea00e6e4d177dca626c7f3633 Mon Sep 17 00:00:00 2001 From: kaixed Date: Fri, 29 Nov 2024 10:07:03 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84conversationAdapt?= =?UTF-8?q?er?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用ListAdapter重构conversationAdapter以提高效率 --- .../kchat/ui/adapter/ConversationAdapter.kt | 78 ++++++++++--------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/com/kaixed/kchat/ui/adapter/ConversationAdapter.kt b/app/src/main/java/com/kaixed/kchat/ui/adapter/ConversationAdapter.kt index dbddc72..bb1f34e 100644 --- a/app/src/main/java/com/kaixed/kchat/ui/adapter/ConversationAdapter.kt +++ b/app/src/main/java/com/kaixed/kchat/ui/adapter/ConversationAdapter.kt @@ -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, private val context: Context -) : RecyclerView.Adapter() { + private val context: Context +) : ListAdapter(ConversationDiffCallback()) { - private var onChatListItemClickListener: OnChatListItemClickListener? = null - - companion object { - private const val TAG = "ConversationAdapter" - } - - fun updateData(newChatLists: List) { - 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() { + override fun areItemsTheSame(oldItem: Conversation, newItem: Conversation): Boolean { + return oldItem.talkerId == newItem.talkerId + } + + override fun areContentsTheSame(oldItem: Conversation, newItem: Conversation): Boolean { + return oldItem == newItem + } + } }