Last active
January 9, 2024 07:05
-
-
Save shikto1/09af5cb9b6a99e79dd58bdea8d38abf3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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