RecyclerView notifyItemRangeChanged不显示新数据 [英] RecyclerView notifyItemRangeChanged doesn't show new data

查看:1278
本文介绍了RecyclerView notifyItemRangeChanged不显示新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了关于notifyItemRangeChangedRecyclerView#Adapter问题.似乎如果Adapter认为从上次调用到getItemCount的大小为0,然后我调用Adapter#notifyItemRangeChanged(0, 1),则Adapter只会忽略该调用(它不会导致例如,要插入的新项目).

I am running into an issue with the RecyclerView#Adapter regarding notifyItemRangeChanged. It seems that if the Adapter thinks it has a size of 0 from the last call to getItemCount, and then I call Adapter#notifyItemRangeChanged(0, 1), the Adapter will simply ignore the call (it doesn't result in the new item being inserted, for example).

这是预期的行为吗?

推荐答案

这是预期的行为吗?

Is this the expected behavior?

简短的回答:是的.

notifyDataSetChanged() (是的,我知道有不同的方法,但是在这里只引用它是因为它解释了项目更改结构更改之间的区别):

数据更改事件有两种不同的类别,即项目更改和结构更改.物料更改是指单个物料的数据已更新但未发生位置更改的情况.结构更改是指在数据集中插入,删除或移动项目的时间.

There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred. Structural changes are when items are inserted, removed or moved within the data set.

现在,请通读 notifyItemRangeChanged() (我的重点):

Now have a read through the documentation on notifyItemRangeChanged() (my emphasis):

这是项目更改事件,而不是结构更改事件..它表示在给定位置范围内对数据的任何反映都已过时,应进行更新.给定范围内的项目保持相同的身份.

This is an item change event, not a structural change event. It indicates that any reflection of the data in the given position range is out of date and should be updated. The items in the given range retain the same identity.

那应该回答你的问题.您正在进行结构更改(也就是说,您要添加项目),因此notifyItemRangeChanged()不是 合适的调用方法.相反,您应该调用 notifyItemRangeInserted() (或它的单数形式),它确实表明已进行了结构更改.

That should answer your question. You're making a structural change (that is, you're adding an item), hence notifyItemRangeChanged() is not the appropriate method to call. Instead, you should call notifyItemRangeInserted() (or its singular equivalent), which does indicate a structural change was made.

这篇关于RecyclerView notifyItemRangeChanged不显示新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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