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年10月7日
    3
  • 运维架构层级

    运维架构层级运维架构层级/运维角度内容描述/主要技术关键词监控体系自动化/DevOps云计算客户端层浏览器Cookie、浏览器缓存协商(Last-Modified、Expires、Etag)、组件分离、前端优化、运维检测工具舆论监控外部网络监控APM故障检测工具DNS服务CDN服务移动服务云

    2022年7月17日
    18
  • mysql 1142错误_mysql 1067错误

    mysql 1142错误_mysql 1067错误环境:CentOS7+mysql5.7在创建视图中,出现了1142报错信息,如下:报错原因:使用的mysql用户没有创建视图的权限解决:赋予该用户权限,我这里使用的用户名是admin,1、通过root进入mysql执行一下语句mysql>grantallprivilegesonshop.*toadmin;QueryOK,0rowsaffected(0.00sec)mysql>flushprivileges;QueryOK,0rowsaf

    2022年10月1日
    2
  • HttpCanary下载_用java编写自我介绍

    HttpCanary下载_用java编写自我介绍前言首先,我们无论学习哪个框架,都要带着问题,带着思考去学习思考1:HttpRunner是什么?思考2:HttpRunner的设计模式是什么?思考3:为什么我们要学习HttpRunner?他的

    2022年7月28日
    5
  • 表格存储:使用TableStoreWriter进行高并发、高吞吐的数据写入

    表格存储:使用TableStoreWriter进行高并发、高吞吐的数据写入

    2021年9月15日
    65
  • W5500问题集锦(持续更新中)

    W5500问题集锦(持续更新中)

    2021年11月14日
    76

发表回复

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

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