0

我使用 google 建议使用 diff utils 创建我的回收站视图,但 diff utils 没有使用我的 livedata 列表更新我已经尝试了一切

我的模型课

data class Note(
    val Title:String?=null,//column 2
    val Note:String?=null// column 3
):Serializable{
    @PrimaryKey(autoGenerate = true)
    var id:Int=0
}

我的 Recyclerview 适配器


    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)
            }
        })
    }

它只是观察何时将新数据添加到房间数据库,但当我删除时不会计算列表没有任何反应,我将不胜感激

4

0 回答 0