RecyclerView Adapter中notifyDataSetChanged 的作用
生活随笔
收集整理的這篇文章主要介紹了
RecyclerView Adapter中notifyDataSetChanged 的作用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一直認(rèn)為notifyDataSetChanged? 是 用來刷新數(shù)據(jù)的 當(dāng)數(shù)據(jù)發(fā)生變化的時(shí)候調(diào)用notifyDataSetChanged?
官方說:通知任何注冊(cè)的觀察者數(shù)據(jù)發(fā)生了改變(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 是一個(gè)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. 通知任何注冊(cè)的觀察者數(shù)據(jù)發(fā)生了改變
?
?
總結(jié)
以上是生活随笔為你收集整理的RecyclerView Adapter中notifyDataSetChanged 的作用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 减肥训练营多少钱啊?
- 下一篇: 尾径30以上的柏树原木要多少钱一立方