Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It will always be more efficient to use more specific change events if you can. Rely on notifyDataSetChanged as a last resort. RecycleView

after update android studio to arctic fox, I get this warning. But I dont know what is the efficient way to notify data change. in my code I'm filling the adapter from network call and then I notifydatasetchange, but the compiler gave me this:

It will always be more efficient to use more specific change events if you can. Rely on notifyDataSetChanged as a last resort. RecycleView

edit question: the want us to use

DiffUtil docs

instead of notifyDataSetChanged() because it much faster. check this article on medium.

like image 608
Mostafa Onaizan Avatar asked Jul 31 '21 12:07

Mostafa Onaizan


People also ask

What can I use instead of notifyDataSetChanged?

When you call notifyItemChanged(mPos) , it is equivalent to a call to notifyItemRangeChanged(mPos, 1) , and every time it is called, requestLayout() is also called. On the other hand, when you call notifyDataSetChanged() or notifyItemRangeChanged(0, mList. size()) , there is only one call to requestLayout() .

What is the use of notifyDataSetChanged in Android?

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

When onCreateViewHolder is Called?

onCreateViewHolder is called when you need a new View. by Pavlos-Petros Tournaris | Medium. This is not the way a recycling works. onCreateViewHolder is called when you need a new View.


3 Answers

It means that if you need to change the whole item list at once in the recyclerview, then use notifyDataSetChanged().

If you need to change the specific item, then it's better to use notifyItemChanged(position) so that it won't refresh & rebind the whole dataset which can impact the performance if the dataset is large.

So it's just a normal suggestion or maybe a warning, nothing to worry about. :)

like image 90
Dev4Life Avatar answered Nov 02 '22 06:11

Dev4Life


The function notifyDataSetChanged essentially considers all data in your dataset has changed. This causes all VISIBLE views using this data to be redrawn. This is unnecessary when only some data has changed.

You need to identify the position that data has change and notify your adapter to update only those items.

you can notify change of the particular position using this methods

  1. notifyItemChanged(int)
  2. notifyItemInserted(int)
  3. notifyItemRemoved(int)
  4. notifyItemRangeChanged(int, int)
  5. notifyItemRangeInserted(int, int)
  6. notifyItemRangeRemoved(int, int)
like image 45
Mittal Varsani Avatar answered Nov 02 '22 06:11

Mittal Varsani


It's just a suggestion by Android studio to update data in RecyclerView.

This advice to notify change in the Recycler view items using particular postion update only like, notifyItemChanged(int), notifyItemInserted(int), notifyItemRemoved(int) ,notifyItemRangeChanged(int, int), notifyItemRangeInserted(int, int), notifyItemRangeRemoved(int, int).

notifyDataSetChanged() should be used as the last resort or as the final course of action. This function will reinitialize and rebind all views once again which can decrease the performance.

like image 27
Bharat Lalwani Avatar answered Nov 02 '22 06:11

Bharat Lalwani