Android RecyclerView notifyItemRangeChanged 与 notifyItemChanged

Android RecyclerView notifyItemRangeChanged vs notifyItemChanged

我有一个数据集,它可能会在刷新时发生变化。很难确定现有数据集中的哪些条目已更改。我通读了 recyclerview 适配器说明,我的主要问题是。

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

稍微复杂一点,因为根据更改的内容,每个视图的顺序可能会发生变化,那么它是调用 notifyItemRangeInserted 的参数,针对受影响的范围调用 notifyItemRangeRemoved,还是一个一个地调用 notifyItemMoved?

哪种方式效率更高?

谢谢

RecyclerView 不会'yet'尽可能合并这些更改事件,尽管这是我们正在考虑的事情(低优先级)。

除非你正在调度数百个事件,否则应该没问题,但如果你可以调度 Range 个事件,当然对 RecyclerView 更好。

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