Skip to content

Instantly share code, notes, and snippets.

@shikto1
Last active January 9, 2024 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shikto1/09af5cb9b6a99e79dd58bdea8d38abf3 to your computer and use it in GitHub Desktop.
Save shikto1/09af5cb9b6a99e79dd58bdea8d38abf3 to your computer and use it in GitHub Desktop.
1. Use Image Libraries: If need to load image in itemview, use Glide, Picasso or Fresco library.
2. setHasStableIds: This method should be used setHasStableIds(true) to enable stable item IDs. This helps in efficiently
updating and reordering items without unnecessary rebinds.
3. setHasFixedSize: If RecyclerView size itself is fixed and wouldn’t change due to its content and,
using setHasFixedSize(true) can help improve performance by avoiding unnecessary layout calculations.
4. setItemViewCacheSize: Adjust the cache size using this method to control how many offscreen views are
retained. This can help in managing memory usage.
5. Avoid a nested view: If possible, we should avoid a nested view and try to create a flat view wherever
possible. Nesting reduces RecyclerView performance. Flat View improves performance.
6. Make sure onBindViewHolder() is as cheap as possible. Remember that onBindViewHolder() is called frequently.
7. When data gets changed, try to update only the affected items. For example instead of invalidating the
whole data set with notifyDataSetChanged(), when adding / loading more items, just use:
adapter.notifyItemRangeInserted(rangeStart, rangeEnd);
adapter.notifyItemRemoved(position);
adapter.notifyItemChanged(position);
adapter.notifyItemInserted(position);
Rely on notifyDataSetChanged() as a last resort.
But if you need to use it, maintain your items with unique ids:
adapter.setHasStableIds(true);
8. Use setRecycledViewPool for Optimizing Nested RecyclerView.
DiffUtils is Utility Tool we can use optimize out recyclerview.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment