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