我不知道这是否是最好的方法,但这就是我一直以来的工作方式,并且没有给我带来任何问题。
class DiffUtiltsGeneric<T>(val oldList : List<T>,val newList : List<T>) : DiffUtil.Callback() {
companion object{
private const val TAG = "DiffUtiltsGeneric"
}
override fun getOldListSize(): Int = oldList.size
override fun getNewListSize(): Int = newList.size
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean =
return oldList[oldItemPosition] == newList[newItemPosition]
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean =
oldList[oldItemPosition] == newList[newItemPosition]
}
这里我给你留下一个 DiffUtils,用于列出通用 RecyclerView
那么您所要做的就是按以下顺序:
计算两个列表的差异
更新您在回收站中使用的列表
发送更改
fun updateList(newList : List<RES>)
{
val diffUtils = DiffUtiltsGeneric(resourceListFiltered,newList)
val result = DiffUtil.calculateDiff(diffUtils) //1.
resourceListFiltered = newList //2.
result.dispatchUpdatesTo(this) //3.
}
希望对您以后的实施有所帮助
尝试在您的适配器中使用NotifyDataSetChanged。它更直观、更容易使用,因此不会给您带来这些麻烦