DiffCallback for DrawerLayoutAdapter #1842
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why notifyDataSetChanged() is Suboptimal
Full Redraw: Calling notifyDataSetChanged() triggers a complete redraw of the entire RecyclerView, even if only a single item has changed.
Resource Intensive: This demands significant computational resources, particularly for large lists, as all items must be re-bound and re-drawn.
Undesirable Animations: A full redraw resets any animations associated with list items, leading to a visually jarring user experience.
Why DiffUtil is More Optimal
Precise Updates: DiffUtil calculates the minimal set of changes (additions, removals, changes, moves) required to transform the old list into the new list.
Targeted Operations: Based on these changes, the adapter uses notifyItemInserted(), notifyItemRemoved(), notifyItemChanged(), and notifyItemMoved() to update only the items that actually need updating.
Improved Performance: This significantly reduces the load on the system, resulting in a smoother and more responsive UI, especially with large lists or frequent data updates.
Preservation of Animations: DiffUtil preserves RecyclerView item animations, providing a more polished visual experience.
In summary, DiffUtil provides granular control over RecyclerView updates, minimizing the amount of work needed to refresh displayed data, in contrast to the all-or-nothing approach of notifyDataSetChanged().