RecyclerView notifyItemRangeChanged 不显示新数据

RecyclerView notifyItemRangeChanged doesn't show new data

我 运行 遇到了关于 notifyItemRangeChangedRecyclerView#Adapter 问题。似乎如果 Adapter 认为它从上次调用 getItemCount 开始的大小为 0,然后我调用 Adapter#notifyItemRangeChanged(0, 1),则 Adapter 将简单地忽略调用(例如,它不会导致插入新项目)。

这是预期的行为吗?

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