我使用Google推荐的diff utils创建了回收站视图,但diff utils并没有更新我尝试过的所有活动数据列表
我的模型班
data class Note(
val Title:String?=null,//column 2
val Note:String?=null// column 3
):Serializable{
@PrimaryKey(autoGenerate = true)
var id:Int=0
}
我的Recylerview适配器
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item=getItem(position)
holder.bind(item,clickLitener)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder.from(parent)
}
}
class ViewHolder constructor(val binding: NoteLayoutBinding):RecyclerView.ViewHolder(binding.root){
fun bind(item:Note,clickLitener: NoteClickLitener){
binding.noteDB=item
binding.clicklistener=clickLitener
binding.executePendingBindings()
}
companion object{
fun from(parent: ViewGroup):ViewHolder{
val layoutInflater=LayoutInflater.from(parent.context)
val binding=NoteLayoutBinding.inflate(layoutInflater,parent,false)
return ViewHolder(binding)
}
}
}
class NoteAdapterDiffUtils :DiffUtil.ItemCallback<Note>(){
override fun areItemsTheSame(oldItem: Note, newItem: Note): Boolean {
return oldItem.id==newItem.id
}
override fun areContentsTheSame(oldItem: Note, newItem: Note): Boolean {
return oldItem==newItem
}
}
class NoteClickLitener(val clicklistener: (Note)->Unit){
fun onClick(Note:Note)=clicklistener(Note)
主要片段
private fun subscribeObserver(){
viewModel.loadData().observe(viewLifecycleOwner, Observer {
it.let {
adapter.submitList(it)
}
})
}
它只是观察何时将新数据添加到room db,但是当我删除任何内容时都不会计算列表,请多加帮助