Recyclerview DiffUtil 项目更新

Recyclerview DiffUtil Item Update

我的 recyclerview 无限滚动,因此,当有新数据时它会更新。我正在使用 DiffUtil 更新 recyclerview 中的数据。 DiffUtil 确实会更新数据,但只要有更新数据,recyclerview 就会滚动到顶部,看起来像 "using the notifydatasetchanged()"。这是我的 DiffUtil 和我用来更新数据的适配器。

class ProductDiffUtil(
    val oldProductList: List<ProductModel>, val newProductList: List<ProductModel>
) : DiffUtil.Callback() {

    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return oldProductList[oldItemPosition].id == newProductList[newItemPosition].id
    }

    override fun getOldListSize(): Int = oldProductList.size

    override fun getNewListSize(): Int = newProductList.size

    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return oldProductList[oldItemPosition] == newProductList[newItemPosition]
    }

    override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
        return super.getChangePayload(oldItemPosition, newItemPosition)
    }
}

这是我更新数据的适配器

fun addProductList(productList: List<ProductModel>?) {
        val diffResult = DiffUtil.calculateDiff(ProductDiffUtil(this.productList, productList!!))
        this.productList.addAll(productList)
        diffResult.dispatchUpdatesTo(this)
    }

请帮我解决这个问题。当我使用 notifyItemRangeChanged() 时它工作正常...所以我应该使用什么来更新 recyclerview 中的数据以获得最佳实践。

https://drive.google.com/open?id=1SapXW2wusmTpyGCRA9fa0aSLCYNL1fzN

检查布局管理器是否已经设置并获取当前滚动位置。像这样:

var itemPostion= 0
if(myRecyclerView.layoutmanager != null){
  itemPostion = (myRecyclerView.layoutmanager as LinearLayoutManager)
.findFirstCompletelyVisibleItemPosition()
}

您可以在 GitHub

上查看此示例项目

您只是将以前的内容与新项目进行比较,而不是与添加了所有项目的列表进行比较。

想象一下,如果 this.productList 当前是 1,2,3,而新的 productList4,5,6。当你 运行

DiffUtil.calculateDiff(ProductDiffUtil(this.productList, productList!!)

它将比较1425等,并得出结论,一切都变了,没有添加新项目。 (注意:这是对 DiffUtil 算法的过度简化,但可以说明这一点)

相反,如果您想使用 DiffUtil:

val oldList = ArrayList(productList)
this.productList.addAll(productList)
val diffResult = DiffUtil.calculateDiff(ProductDiffUtil(oldList, productList!!)
diffResult.dispatchUpdatesTo(this)

或者,因为您确切知道添加了多少项以及添加到何处,只需使用 notifyItemRangeInserted 并避免复制:

val oldSize = this.productList.size
this.productList.addAll(productList)
notifyItemRangeInserted(oldSize, productList.size)

考虑制作一个通用的 diffUtil class,而不是为每个适配器创建它。

fun <T>diffList(oldList: List<T>, newList: List<T>, sameItem: (a: T, b: T) -> Boolean): DiffUtil.DiffResult {
val callback: DiffUtil.Callback = object : DiffUtil.Callback() {
    override fun getOldListSize(): Int {
        return oldList.size
    }

    override fun getNewListSize(): Int {
        return newList.size
    }

    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return sameItem(oldList[oldItemPosition], newList[newItemPosition])
    }

    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return oldList[oldItemPosition] == newList[newItemPosition]
    }
}

return DiffUtil.calculateDiff(callback) }

您可以像这样在您的适配器中使用它:

 fun setItems(products: List<Product>) {
    val oldList = productList
    productList = products
    diffList(oldList, products, sameItem = { a, b -> a.id == b.id }).dispatchUpdatesTo(this)
}