RecyclerView Adapter中notifyDataSetChanged 的作用

RecyclerView Adapter中notifyDataSetChanged 的作用一直认为notifyDataSetChanged是用来刷新数据的当数据发生变化的时候调用notifyDataSetChanged官方说:通知任何注册的观察者数据发生了改变(Notifyanyregisteredobserversthatthedatasethaschanged)–自己翻译的不保证完全正确。。。。。。今天有空翻阅下源码publicfin…

大家好,又见面了,我是你们的朋友全栈君。

一直认为notifyDataSetChanged  是 用来刷新数据的 当数据发生变化的时候调用notifyDataSetChanged 

官方说:通知任何注册的观察者数据发生了改变(Notify any registered observers that the data set has changed) –自己翻译的不保证完全正确。。。。。。

今天有空翻阅下源码

public final void notifyDataSetChanged() {
    mObservable.notifyChanged();
}

在看看 notifyChanged()方法

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();
    }
}

这里的mObservers 是一个list 

protected final ArrayList<T> mObservers = new ArrayList<T>();

这样看来notifyDataSetChanged 是改变list 的元素

在看下源码的注释 

相似的方法

* @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 data set has changed.
 *
 * <p>There are two different classes of data change events, item changes and structural
 * changes. Item changes are when a single item has its data updated but no positional
 * changes have occurred. Structural changes are when items are inserted, removed or moved
 * within the data set.</p>
 *
 * <p>This event does not specify what about the data set has changed, forcing
 * any observers to assume that all existing items and structure may no longer be valid.
 * LayoutManagers will be forced to fully rebind and relayout all visible views.</p>
 *
 * <p><code>RecyclerView</code> will attempt to synthesize visible structural change events
 * for adapters that report that they have {@link #hasStableIds() stable IDs} when
 * this method is used. This can help for the purposes of animation and visual
 * object persistence but individual item views will still need to be rebound
 * and relaid out.</p>
 *
 * <p>If you are writing an adapter it will always be more efficient to use the more
 * specific change events if you can. Rely on <code>notifyDataSetChanged()</code>
 * as a last resort.</p>

可以看到官方说 notifyDataSetChanged 的作用

Notify any registered observers that the data set has changed. 通知任何注册的观察者数据发生了改变

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150104.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Win10自动更新怎么永久关闭?有效的Win10强制更新关闭方法

    Win10自动更新怎么永久关闭?有效的Win10强制更新关闭方法之前小编为大家分享过一些Win10彻底关闭WindowsUpdate自动更新的方法,主要是通过一些如设置流量计费或借助一些专门的小工具来实现,但往往会发现,Win10自动更新就像打不死的小强,不管怎么关闭,之后还是会自动更新,让不少小伙伴颇为不爽。今天小编带来了这篇改进型教程,通过全方位设置,彻底关闭Win10自动更新,感兴趣的小伙伴不妨试试吧。…

    2022年6月18日
    35
  • this关键字与super关键字详解

    this关键字与super关键字详解一.this关键字1.实例一:(1)需求:使用Java类描述一个动物;(2)实例:classAnimal{ Stringname; //成员变量 Stringcolor; publicAnimal(Stringn,Stringc){ name=n; color=c; } publicvoideat(){ Stringname=

    2022年6月15日
    33
  • matlab维纳滤波器函数_无源定位matlab源程序

    matlab维纳滤波器函数_无源定位matlab源程序维纳滤波器fir滤波器程序clear;clc;%输入信号A=1;%信号的幅值f=1000;%信号的频率fs=10^5;…

    2025年5月22日
    8
  • navicat激活码最新(JetBrains全家桶)

    (navicat激活码最新)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月30日
    77
  • php集成环境总结(php新手)

    php集成环境总结(php新手)1、WampServer Wamp就是WindowsApacheMysqlPHP集成安装环境,即在window下的apache、php和mysql的服务器软件。PHP扩展、Apache模块,开启/关闭鼠标点点就搞定,再也不用亲自去修改配置文件了,WAMP它会去做。再也不用到处询问php的安装问题了,WAMP一切都搞定了,这个软件在win平台上使用的较多。

    2022年6月28日
    28
  • top命令详解(转载)「建议收藏」

    top命令详解(转载)「建议收藏」top命令用法top命令经常用来监控linux的系统状况,是常用的性能分析工具,能够实时显示系统中各个进程的资源占用情况。top的使用方式top[-dnumber]|top[-bnp]参数解释:-d:number代表秒数,表示top命令显示的页面更新一次的间隔。默认是5秒。-b:以批次的方式执行top。-n:与-b配合使用,表示需要进行几次top命令的输出结果。-p:指定特定的pid进程号进行观察。在top命令显示的页面还可以输…

    2022年9月24日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号