ter方法和notifydatasetchanged方法有什么区

ter方法和notifydatasetchanged方法有什么区

本文介绍了Recycler视图中的swapadapter方法和notifydatasetchanged方法有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道RecylerView的swapAdapter和notifyDatasetChanged方法之间到底有什么区别?修改数据时,哪一种更好?

I would like to know what exactly is the difference between swapAdapter and notifyDatasetChanged methods of RecylerView? Which one is better to use while modifying the data?

推荐答案

文档阅读时.

public void swapAdapter (Adapter adapter, boolean removeAndRecycleExistingViews)

用提供的适配器交换当前适配器.它与setAdapter(Adapter)相似,但是假定现有适配器并且新适配器使用相同的RecyclerView.ViewHolder,并且不会清除RecycledViewPool.

Swaps the current adapter with the provided one. It is similar to setAdapter(Adapter) but assumes existing adapter and the new adapter uses the same RecyclerView.ViewHolder and does not clear the RecycledViewPool.

请注意,它仍然会调用onAdapterChanged回调.

Note that it still calls onAdapterChanged callbacks.

以及

public final void notifyDataSetChanged ()

通知所有注册观察者该数据集已更改.

Notify any registered observers that the data set has changed.

数据更改事件有两种不同的类别,即项目更改和结构更改.物料更改是指单个物料的数据已更新但未发生位置更改的情况.结构更改是指在数据集中插入,删除或移动项目的时间.

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.

此事件未指定数据集已更改的内容,从而迫使所有观察者假定所有现有项目和结构可能不再有效. LayoutManager将被迫完全重新绑定并重新布局所有可见视图.

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.

RecyclerView将尝试为使用此方法报告其具有稳定ID的适配器合成可见的结构更改事件.这可以帮助实现动画和视觉对象的持久性,但是仍然需要重新放置和重新布局单个项目视图.

RecyclerView will attempt to synthesize visible structural change events for adapters that report that they have 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.

如果您正在编写适配器,那么如果可以的话,使用更具体的更改事件将总是更加高效.作为最后的手段,依靠notifyDataSetChanged().

If you are writing an adapter it will always be more efficient to use the more specific change events if you can. Rely on notifyDataSetChanged() as a last resort.

我感觉文档很好地说明了区别所在,而swapAdapter(ad,true)是更改数据的一种方式,而notifyDataSetChanged()是通知适配器在数据具有后重绘其视图的方法已更改.

Well i feel the documentation lays it out nicely as to where the difference lies and swapAdapter(ad,true) is a way to change the data whereas notifyDataSetChanged() is a method to notify adapter to redraw its views after the data has been changed.

这篇关于Recycler视图中的swapadapter方法和notifydatasetchanged方法有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:04