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.
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() .
notifyDataSetChanged. Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
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.
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. :)
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With