Mohamed hassan
Mohamed hassan

Reputation: 61

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.

Upvotes: 5

Views: 4450

Answers (3)

Sagar
Sagar

Reputation: 24937

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.

Upvotes: 2

Ben P.
Ben P.

Reputation: 54244

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.

Upvotes: 5

creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126914

notifyItemInserted(int position) takes the position of your inserted item as an argument, notifies about that item insert and thus also shifts positions after that item.

notifyDataSetChanged() notifies that the data set connected to the adapter has changed.

Upvotes: 1

Related Questions