Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

notifyDataSetChanged vs notifyItemInserted

I'm confused if we use the two methods to tell the adapter that data you point were changed so what is the difference between them.

like image 324
Mohamed hassan Avatar asked Feb 20 '18 22:02

Mohamed hassan


2 Answers

notifyDataSetChanged() can be thought of as a "major" change. You're telling the adapter that everything in the data set has changed, and so it should re-bind every single child.

notifyItemInserted() (and the other methods like notifyItemRemoved() etc) can all be thought of as "minor" changes. You're telling the adapter exactly how the data set has changed, and so it can perform optimizations (like only re-binding the affected children).

Notably, using the "minor" change methods will also give you nice animations by default, which makes it a lot easier for the user to see what changed in the list.

like image 99
Ben P. Avatar answered Oct 17 '22 09:10

Ben P.


Based on the documentation

notifyDataSetChanged():

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.

notifyItemInserted()

Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.

Main difference is that notifyDataSetChanged() will cause more overhead since it will force LayouManagers to full rebind the views where as notifyItemInserted() will not rebound all the views again but rather alter positions for them.

For better performance, rely on notifyDataSetChanged() as a last resort. Use the more specific change events (like notifyItemInserted()) wherever possible for better efficiency.

like image 31
Sagar Avatar answered Oct 17 '22 09:10

Sagar