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


相关推荐

  • ascii码表完整版_ascii码表高清

    ascii码表完整版_ascii码表高清ASCII码表ASCII值控制字符ASCII值控制字符ASCII值控制字符ASCII值控制字符0NUL32(space)64@96、1SOH33!65A97a2STX34”66B98b3ETX35#6

    2022年8月4日
    9
  • 方法引用

    方法引用

    2021年11月12日
    41
  • socket编程原理「建议收藏」

    socket编程原理「建议收藏」socket编程原理1、问题的引入1)普通的I/O操作过程:UNIX系统的I/O命令集,是从Maltics和早期系统中的命令演变出来的,其模式为打开一读/写一关闭(open-write-read-c

    2022年7月2日
    20
  • 【每天一个 Linux 命令】tree命令

    【每天一个 Linux 命令】tree命令1.前言本文主要讲解Linux系统上的tree命令的详细使用方法。tree命令是一个小型的跨平台命令行程序,用于递归地以树状格式列出或显示目录的内容。它输出每个子目录中的目录路径和文件,以及子目录和文件总数的摘要。tree程序可以在Unix和类Unix系统(如Linux)中使用,也可以在DOS、Windows和许多其他操作系统中使用。它为输出操作提供了各种选项,从文件选项、排序选项到图形选项,并支持XML、JSON和HTML格式的输出。在这篇教程中,我们将通过使用案例演示如何使用tree命令递归

    2022年7月24日
    11
  • c++map的基本使用和操作

    c++map的基本使用和操作

    2021年5月19日
    243
  • 用户自定义类加载器下载_spring类加载器顺序

    用户自定义类加载器下载_spring类加载器顺序在Java的日常应用程序开发中,类的加载几乎是由上述3种类加载器相互配合执行的,在必要时,我们还可以自定义类加载器,来定制类的加载方式。为什么要自定义类加载器?隔离加载类 修改类加载的方式 扩展加载源 防止源码泄漏用户自定义类加载器实现步骤:开发人员可以通过继承抽象类ava.1ang.ClassLoader类的方式,实现自己的类加载器,以满足一些特殊的需求 在JDK1.2之前,在自定义类加载器时,总会去继承ClassLoader类并重写1oadClass()方法,从而实现自定义的类加载类

    2025年8月28日
    4

发表回复

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

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