Android RecyclerView notifyItemRangeChanged与notifyItemChanged

And*_*idP 2 performance android android-animation android-recyclerview

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

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

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

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

谢谢

Tre*_*Cai 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);
    }
Run Code Online (Sandbox Code Playgroud)

所以,notifyItemChanged(int) 是的 notifyItemRangeChanged(int, int)