大家好,又见面了,我是你们的朋友全栈君。
一直认为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. 通知任何注册的观察者数据发生了改变
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/150104.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...