Android RecyclerView notifyItemRangeChanged与notifyItemChanged

时间:2015-02-26 13:04:33

标签: android performance android-animation android-recyclerview

我有一个数据集,可能会在刷新时发生变化。确定现有数据集中的哪些条目已被更改是非常不可预测的。我通读了recyclerview适配器描述和我遇到的主要问题。

更有效地确定哪些数据视图已更改,然后对受影响的范围使用notifyItemRangeChanged,还是应该在每次更改数据视图后逐个更改它们然后再更改notifyItemChanged?

稍微复杂一点,因为根据改变的内容,每个视图的顺序可能已经改变,然后它是为受影响的范围调用notifyItemRangeInserted,notifyItemRangeRemoved或逐个调用notifyItemMoved的参数?

这种方式或其他方式更有效吗?

由于

2 个答案:

答案 0 :(得分:5)

这是源代码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)

答案 1 :(得分:2)

RecyclerView还没有&#39;尽可能合并这些变更事件虽然这是我们正在考虑的事情(低优先级)。

除非您要发送数百个事件,否则它应该没问题,但如果您可以发送Range事件,那么对RecyclerView来说当然更好。