我正在创建一个 RecyclerView 适配器。但后来我遇到了 AsyncListDiffer 问题。
无法使用提供的参数调用以下函数 - AsyncListDiffer
如何解决?
NoteAdapter.kt
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.mahi.notesapp.databinding.NoteLayoutBinding
import com.mahi.notesapp.model.Note
class NoteAdapter: RecyclerView.Adapter<NoteAdapter.NoteViewHolder>()
{
class NoteViewHolder(val itemBinding: NoteLayoutBinding): RecyclerView.ViewHolder(itemBinding.root)
{
private val differCallback = object : DiffUtil.ItemCallback<Note>(){
override fun areItemsTheSame(oldItem: Note, newItem: Note): Boolean
{
return oldItem.id == newItem.id &&
oldItem.noteBody == newItem.noteBody &&
oldItem.noteTitle == newItem.noteTitle
}
override fun areContentsTheSame(oldItem: Note, newItem: Note): Boolean {
return oldItem == newItem
}
}
val differ = AsyncListDiffer(this, differCallback )
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteViewHolder
{
return NoteViewHolder(NoteLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))
}
override fun getItemCount(): Int {
TODO("Not yet implemented")
}
override fun onBindViewHolder(holder: NoteViewHolder, position: Int)
{
// val currentNote = differ.currentList[position]
//
// holder.itemBinding.tvNoteTitle
}
}
将 differentCallback 和 AsyncListDiffer 从 ViewHolder 中移出并放入 Adapter 中。 AsyncListDiffer 应该属于 RecyclerView.Adapter,因为它在适配器级别管理数据,而不是 ViewHolder 级别。