【问题标题】:Android RecyclerView notifyItemRangeChanged vs notifyItemChangedAndroid RecyclerView notifyItemRangeChanged vs notifyItemChanged
【发布时间】:2015-02-26 13:04:33
【问题描述】:
我有一个数据集,刷新后可能会发生变化。确定现有数据集中的哪些条目已更改是非常不可预测的。我通读了 recyclerview 适配器说明,我的主要问题是。
确定哪个数据视图已更改,然后对受影响的范围使用 notifyItemRangeChanged 是否更有效,还是应该在更改每个数据视图后一一更改它们然后 notifyItemChanged?
它变得稍微复杂一些,因为根据更改的内容,每个视图的顺序可能已经更改,然后是调用 notifyItemRangeInserted、notifyItemRangeRemoved 为受影响的范围或 notifyItemMoved 的参数?
用一种方法或另一种方法更有效吗?
谢谢
【问题讨论】:
标签:
android
performance
android-animation
android-recyclerview
【解决方案1】:
这里是源代码,v7-22.1.1
/**
* Notify any registered observers that the item at <code>position</code> has changed.
*
* <p>This is an item change event, not a structural change event. It indicates that any
* reflection of the data at <code>position</code> is out of date and should be updated.
* The item at <code>position</code> retains the same identity.</p>
*
* @param position Position of the item that has changed
*
* @see #notifyItemRangeChanged(int, int)
*/
public final void notifyItemChanged(int position) {
mObservable.notifyItemRangeChanged(position, 1);
}
/**
* Notify any registered observers that the <code>itemCount</code> items starting at
* position <code>positionStart</code> have changed.
*
* <p>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.</p>
*
* @param positionStart Position of the first item that has changed
* @param itemCount Number of items that have changed
*
* @see #notifyItemChanged(int)
*/
public final void notifyItemRangeChanged(int positionStart, int itemCount) {
mObservable.notifyItemRangeChanged(positionStart, itemCount);
}
所以,notifyItemChanged(int) 是 notifyItemRangeChanged(int, int)
【解决方案2】:
如果可能的话,RecyclerView '还没有'做任何努力来合并这些更改事件,尽管这是我们正在考虑的事情(低优先级)。
除非您要分派数百个事件,否则应该没问题,但如果您可以分派Range 事件,当然对 RecyclerView 更好。