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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Verilog实现移位寄存器「建议收藏」

    Verilog实现移位寄存器「建议收藏」移位寄存器

    2022年7月16日
    25
  • UFT录制

    UFT录制在惠普实训学习了UFT(UnifiedFunctionalTesting),是一种自动化测试工具,以VBScript为内嵌语言。打开UFT主界面,选择菜单file/new或file/open可以新建或打开UFT测试。在新建的测试界面打开action,使用HP自带的飞机订票系统软件,打开飞机订票系统,然后点击录制record,弹出窗口选择recordandruntesting,单击OK按钮

    2022年5月23日
    80
  • linux重启mysql

    linux重启mysqllinux重启mysql的方法:1、直接使用“servicemysqldrestart”或“servicemysqlrestart”命令重启;2、使用“/etc/init.d/mysqldrestart”命令重启。1、查看mysql版本方法一:status;方法二:selectversion();2、Mysql启动、停止、重启常用命令a、重启1)、使用service启动: 1 2 servicemysqldres.

    2022年10月17日
    3
  • 阿里用什么替代了dubbo_阿里面试必问题:Spring+MyBaits+微服务+Dubbo+Kakfa带解析

    前言很多同学在群里和我抱怨,面试的时候准备的不充分,导致面试结果不理想,也有很多同学苦于没有一份合适的面试指导。针对这些的同学,在这分享总结的Java面试的高频面试题(包括了Java集合,JVM,并发与多线程,Spring,MyBaits,微服务,Dubbo,Kakfa,中间件,Redis,数据库,设计模式等),进行了整理,免费分享给大家,希望大家能带着这些问题和答案解析,能让你进行有针对性行的学…

    2022年4月5日
    340
  • javachar转int_c中int转char

    javachar转int_c中int转charchar类型的数据转换成int类型的数字。本能反应是这么写的。publicstaticvoidmain(String[]args){charc=’1′;//本能反应是这么写的。inti=c;//或者这么写inti1=(int)c;System.out….

    2022年10月2日
    2
  • datagrip 2021 激活码_最新在线免费激活

    (datagrip 2021 激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~FNWC…

    2022年3月30日
    218

发表回复

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

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