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)
上一篇 2022年6月18日 下午8:16
下一篇 2022年6月18日 下午8:16


相关推荐

  • dubbo rpc调用示例_rpc接口是什么意思

    dubbo rpc调用示例_rpc接口是什么意思在国内dubbo成为很多互联网公司高并发分布式场景下rpc框架的首选,dubbo从开源至今经历过蛮多的过程,从开源到中间的停止维护,经过三年的沉寂,2017年9月,阿里巴巴宣布重启dubbo项目。到2018年2月,阿里将dubbo捐献给Apache基金会,随后dubbo经过孵化后顺利成为apache的顶级项目。当然本文的重点不是介绍dubbo的使用,而是介绍如何利用smart-doc工具来生成dubbo的rpc内部接口文档。smart-doc因为其基于注释和java接口定义自动推导的理念,开源以来受到国内

    2022年8月31日
    5
  • Android程序员的进阶之路

    Android程序员的进阶之路本文主要论述的是android程序员的进阶之路,博主本人就是一名android开发攻城狮,所以这里讲述的大多数是android开发攻城狮的技术进阶之路,如有问题请多指正。大家都知道程序员之中有有菜鸟程序员和大神之分,这里我这暂时把android程序员分为几个层次:android初级程序员、android中级程序员、android高级程序员、android技术专家、CTO等等,不同的级别掌握的能力不

    2022年6月14日
    109
  • 细数泄漏过的数据库,目前已知公开的!「建议收藏」

    细数泄漏过的数据库,目前已知公开的!「建议收藏」先看看这张超长图吧,这里为大家提醒,一定要谨慎在网络中留下隐私信息!同时经常改密码。补天漏洞响应平台的攻城狮和程序猿们花了很长时间,整理了这么一张灰常漂亮的图。这里整理的仅仅是已经被公开的部分,实际上地下有很多企业的大量数据库在流传,社工库已经强大到威胁到所有人的隐私信息,信息安全问题急需解决。我们知道还遗漏了很多数据,欢迎大家给我…

    2026年1月23日
    3
  • Office办公软件快捷键大全

    Office办公软件快捷键大全快捷键作用 Ctrl Shift Spacebar 创建不间断空格 Ctrl 连字符 创建不间断连字符 Ctrl B 使字符变为粗体 Ctrl I 使字符变为斜体 Ctrl U 为字符添加下划线 Ctrl Shift 缩小字号 Ctrl Shift gt 增大字号 Ctrl Q 删除段落格式 Ctrl Space

    2026年3月19日
    1
  • 【OpenClaw从入门到精通】第12篇:OpenClaw 2026子代理实战全攻略:用ACP架构搭建多智能体协作网络(附完整代码)

    【OpenClaw从入门到精通】第12篇:OpenClaw 2026子代理实战全攻略:用ACP架构搭建多智能体协作网络(附完整代码)

    2026年3月16日
    4
  • java三元运算符 包机制 字符串连接

    java三元运算符 包机制 字符串连接字符串连接符我们在算术中 最常遇到的是 a b 此时的加号就是数学中所理解的但是 还会起到字符串连接的作用 inta 10 intb 20 System out println a b System out println a b 这两个输出结果是不一样的结果如下图 外链图片转存失败 源站可能有防盗链机制 建议将图片保存下来直接上传 img m8vCclfZ 83 E Java 截图 day11 截图 41

    2026年3月26日
    2

发表回复

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

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