草庐IT

android - RecyclerView notifyItemRangeChanged 不显示新数据

coder 2023-12-07 原文

我遇到了与 RecyclerView#Adapter 有关 notifyItemRangeChanged 的问题。似乎如果 Adapter 从上次调用 getItemCount 时认为它的大小为 0,然后我调用 Adapter# notifyItemRangeChanged(0, 1)适配器 将简单地忽略调用(例如,它不会导致插入新项目)。

这是预期的行为吗?

最佳答案

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() 的文档(我的重点):

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() (或其单数等效项),这确实表明进行了结构更改。

关于android - RecyclerView notifyItemRangeChanged 不显示新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32769009/

有关android - RecyclerView notifyItemRangeChanged 不显示新数据的更多相关文章

随机推荐