RecyclerView 加载更多时遇到的闪烁问题

本文探讨了使用RecyclerView时出现的闪烁问题,并介绍了如何通过采用特定的方法如notifyItemRangeChanged替代notifyDataSetChanged来解决这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RecyclerView 加载更多时遇到的闪烁问题

当用RecyclerView进行加载更多数据的时候,不要使用notifyDataSetChanged()。该方法谁刷新整体的数据集合,所有的view重新刷新,出现了闪烁的问题,从源码进行分析

public abstract static class Adapter<VH extends ViewHolder> {
    ...
     public final void notifyDataSetChanged() {
            mObservable.notifyChanged();
    }
}

 static class AdapterDataObservable extends Observable<AdapterDataObserver> {

     public void notifyChanged() {
            // since onChanged() is implemented by the app, it could do anything, including
            // removing itself from {@link mObservers} - and that could cause problems if
            // an iterator is used on the ArrayList {@link mObservers}.
            // to avoid such problems, just march thru the list in the reverse order.
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onChanged();
            }
        }
 }

从代码可以看出来,该方法是对所有的数据进行了刷新,如果继续追踪下去,其实就是更新数据,之后recyclerView调用了requestLayout()方法
RecyclerView其实提供了更好的方法来针对发生了变化的数据

* @see #notifyItemChanged(int)
* @see #notifyItemInserted(int)
* @see #notifyItemRemoved(int)
* @see #notifyItemRangeChanged(int, int)
* @see #notifyItemRangeInserted(int, int)
* @see #notifyItemRangeRemoved(int, int)

从名称就能看出具体的作用了,我们看看常用的方法

/**
     * Notify any registered observers that the <code>itemCount</code> items starting at
     * position <code>positionStart</code> have changed.
     * Equivalent to calling <code>notifyItemRangeChanged(position, itemCount, null);</code>.
     *
     * <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);
    }

不需要多说看看注释和方法名称就能体会到他的作用了,他只会更新从positionStart开始的itemCount的个数据变化,而之前的view和数据是不会发生变化的。
使用该方法就可以解决闪烁问题了
网上有说使用
(recyclerCategory.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
来解决闪烁问题的,该方法取消了adapter自带的动画效果,但是我测试只需使用notifyItemRangeChanged就可以解决了,无需进行类似的设置。

Vivado2023是一款集成开发环境软件,用于设计和验证FPGA(现场可编程门阵列)和可编程逻辑器件。对于使用Vivado2023的用户来说,license是必不可少的。 Vivado2023的license是一种许可证,用于授权用户合法使用该软件。许可证分为多种类型,包括评估许可证、开发许可证和节点许可证等。每种许可证都有不同的使用条件和功能。 评估许可证是免费提供的,让用户可以在一段时间内试用Vivado2023的全部功能。用户可以使用这个许可证来了解软件的性能和特点,对于初学者和小规模项目来说是一个很好的选择。但是,使用评估许可证的用户在使用期限过后需要购买正式的许可证才能继续使用软件。 开发许可证是付费的,可以永久使用Vivado2023的全部功能。这种许可证适用于需要长期使用Vivado2023进行开发的用户,通常是专业的FPGA设计师或工程师。购买开发许可证可以享受Vivado2023的技术支持和更新服务,确保软件始终保持最新的版本和功能。 节点许可证是用于多设备或分布式设计的许可证,可以在多个计算机上安装Vivado2023,并共享使用。节点许可证适用于大规模项目或需要多个处理节点进行设计的用户,可以提高工作效率和资源利用率。 总之,Vivado2023 license是用户在使用Vivado2023时必须考虑的问题。用户可以根据自己的需求选择合适的许可证类型,以便获取最佳的软件使用体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值