1.Set setHasFixedSize attribute when the recycler view item does not change the sizes of its children at runtime.
1 |
recyclerView.setHasFixedSize(true) |
2. When data gets changed, try to update only the affected items. When adding more items, just use:
1 2 3 4 |
adapter.notifyItemRangeInserted(rangeStart, rangeEnd); adapter.notifyItemRemoved(position); adapter.notifyItemChanged(position); adapter.notifyItemInserted(position); |
notifyDataSetChanged(), RecyclerView.Adapter didn’t assign the same ViewHolder to the original item in the data set. In case of notifyDataSetChanged(), call setHasStableIds(true) on your adapter and override getItemId(int position) to return a unique Id that would represent each Data Item, simply return.So
1 2 3 4 5 |
@Override public long getItemId(int position) { return itemList.get(position).getId(); //or listData.get(position).hashCode().toLong() } |
3. For RecyclerView with Nested RecyclerViews, setRecycledViewPool allows multiple RecyclerViews to share a common pool of scrap views. This can be useful if you have multiple RecyclerViews with adapters that use the same view type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class OuterAdapter extends RecyclerView.Adapter<OuterAdapter.ViewHolder> { RecyclerView.RecycledViewPool sharedPool = new RecyclerView.RecycledViewPool(); @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // inflate inner item, find innerRecyclerView by ID… LinearLayoutManager innerLLM = new LinearLayoutManager(parent.getContext(), LinearLayoutManager.HORIZONTAL,false); innerRv.setLayoutManager(innerLLM); innerRv.setRecycledViewPool(sharedPool); return new OuterAdapter.ViewHolder(innerRv); } } |
4. RecyclerView Create taking too long
If your view taking a too long time to inflate, look at reducing the cost of your inflation. Reducing unnecessary container and structural Views can help – consider building itemViews
with ConstraintLayout, which can make it easy to reduce structural Views