Recyclerview DiffUtil项目更新 [英] Recyclerview DiffUtil Item Update

查看:107
本文介绍了Recyclerview DiffUtil项目更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have endless scroll in my recyclerview, so, it will update when there is new data. and i am using DiffUtil to update data in the recyclerview. DiffUtil does updates the data but whenever there is update data, recyclerview scroll to top and what it looks like is "using the notifydatasetchanged()". here is my DiffUtil and my adapter to update data.

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

这是我的适配器来更新数据

Here is my adapter to update data

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

请帮助我.当我使用notifyItemRangeChanged()时,它工作正常.因此,为了最佳实践,应该使用什么来更新recyclerview中的数据.

please help me with this. it is working fine when i am using notifyItemRangeChanged()... so what should i use to update data in recyclerview for best practice.

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

推荐答案

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

You're comparing the previous contents against only the new items, rather than against the list with all of them added.

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

Imagine if this.productList is currently 1,2,3, and the new productList is 4,5,6. When you run

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

它将比较 1 4 2 5 等,并得出结论,一切都已更改并且没有添加任何新项目.(注意:这是对DiffUtil算法的过度简化,但只是说明了这一点)

It will compare 1 to 4, 2 to 5, etc. and conclude that everything has changed and no new items have been added. (note: this is an oversimplification of the DiffUtil algorithm, but serves to illustrate the point)

相反,如果要使用DiffUtil:

Instead, if you want to use DiffUtil:

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

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

or, since you know exactly how many items are added and where, just use notifyItemRangeInserted and avoid the copy:

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

这篇关于Recyclerview DiffUtil项目更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆