Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between swapadapter method and notifydatasetchanged method in Recycler view?

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?

like image 764
Jayakrishnan Salim Avatar asked Aug 17 '15 12:08

Jayakrishnan Salim


People also ask

What does notifyDataSetChanged do in RecyclerView?

What does notifyDataSetChanged() do on recyclerview ? 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.

What happens when notifyDataSetChanged is called?

notifyDataSetChanged. Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

What is the difference between list view and RecyclerView?

Simple answer: You should use RecyclerView in a situation where you want to show a lot of items, and the number of them is dynamic. ListView should only be used when the number of items is always the same and is limited to the screen size.


1 Answers

As the Documentation reads.

public void swapAdapter (Adapter adapter, boolean removeAndRecycleExistingViews)

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.

Note that it still calls onAdapterChanged callbacks.

and as for

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.

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 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.

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.

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.

like image 167
harshitpthk Avatar answered Sep 19 '22 19:09

harshitpthk