AndroidP
AndroidP

Reputation: 771

Android RecyclerView notifyItemRangeChanged vs notifyItemChanged

I have a dataset that possibly changes upon refresh. It's quite unpredictable to determine which entries in the existing dataset has been changed. I read through the recyclerview adapter desciptions and the main question I have is.

Is it more efficient figuring out which data-view changed, then use notifyItemRangeChanged for the affected range or should I change them one by one then notifyItemChanged after each data-view has been changed?

It gets slightly further complicated because depending on what was changed, the ordering of each view might have changed, then it's the argument of calling notifyItemRangeInserted, notifyItemRangeRemoved for the affected ranges, or notifyItemMoved one by one?

Is it more efficient doing one way or the other?

Thanks

Upvotes: 2

Views: 5541

Answers (2)

Trey Cai
Trey Cai

Reputation: 1195

Here's the source code, 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);
    }

So, notifyItemChanged(int) is notifyItemRangeChanged(int, int)

Upvotes: 6

yigit
yigit

Reputation: 38263

RecyclerView does not 'yet' do any effort to merge these change events if possible although this is something we are considering (low priority).

Unless you are dispatching hundreds of events, it should be OK but of course it is better for RecyclerView if you can dispatch Range events.

Upvotes: 2

Related Questions