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


相关推荐

  • iOS开发常用国外网站清单

    iOS开发常用国外网站清单工欲善其事必先利其器,最近发现临时查找一些东西容易浪费时间,花了点时间整理一下常用的网站,方便以后备用。国内的code4app,ui4app,cocoachina,oschina,csdn就不说了,基本上很好用。不过国外网站上的好东西更多,可惜找起来也更费时间,需要整理一下。主要分开发教程、示例项目、UI设计、问题解决几块。开发教程:即便过了入门阶段,还是要

    2022年7月12日
    14
  • linux export命令找不到_docker执行容器内的shell

    linux export命令找不到_docker执行容器内的shellLinuxexport命令用于设置或显示环境变量。在shell中执行程序时,shell会提供一组环境变量。export可新增,修改或删除环境变量,供后续执行的程序使用。export的效力仅及于该次登陆操作。语法export[-fnp][变量名称]=[变量设置值]参数说明:-f 代表[变量名称]中为函数名称。 -n 删除指定的变量。变量实际上并未删除,只是不会输出到后…

    2022年9月7日
    0
  • Netty in action—Netty中的ByteBuf[通俗易懂]

    Netty in action—Netty中的ByteBuf[通俗易懂]正如之前所说,网络传输的基本单位是字节。JavaNIO提供了ByteBuffer作为它的容器,但是这个类使用起来比较复杂和麻烦。Netty提供了一个更好的实现:ByteBuf。ByteBuf的APINetty为数据处理提供的API通过抽象类ByteBuf和接口ByteBufHolder暴露出来。下面列出ByteBufAPI的优点:可扩展到用户定义的buffer类型中通过内置的复合buf

    2022年9月17日
    0
  • tar压缩隐藏文件「建议收藏」

    tar压缩隐藏文件「建议收藏」如果想tar压缩包含隐藏文件的目录,同时排除掉部分无用的目录tar-czvf20161009.tar.gz*.[!.]* –exclude.git

    2022年5月26日
    178
  • Android打开第三方应用方法总结

    Android打开第三方应用方法总结

    2021年10月1日
    35
  • 微软第一台电脑_世界上第一位皇帝是谁

    微软第一台电脑_世界上第一位皇帝是谁2月9日,历史上的今天,世界上第一位计算机科学博士DavidWheeler出生;BAN逻辑的提出者RogerNeedham出生;人工智能控制论先驱KevinWarwick出生;谷歌发布GoogleBuzz;微软发布SurfacePro。

    2022年10月15日
    0

发表回复

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

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