I have main RecyclerView which contains other RecyclerViews (let's call them subRecyclerViews). The number of subRecyclerViews is based on the data received from server. The problem is, that whenever a subRecyclerView is about to become visible, it creates ViewHolders for all its items at once (instead of creating ViewHolders only for visible items).
In my MainRecyclerViewAdapter onBindViewHolder() method I call
subRecyclerViewAdapter.setData(data);
subRecyclerView.notifyDataSetChanged();
which results in a lag, because the subRecyclerView is calling onCreateViewHolder() and onBindViewHolder() methods for all its items.
The version of RecyclerView I use is
com.android.support:recyclerview-v7:25.1.1
The question is, is there a way to tell subRecyclerView that it doesn't need to create ViewHolders for the items, that are not yet visible? Also, is this a bug in RecyclerView or am I doing something wrong?
If you need a view with different viewTypes, you can just write the Adapters for each section and just use ConcatAdapter to merge all of them inside one recyclerview.
By default it have 5. you can increase as per your need. Save this answer.
You will have to override two main methods: one to inflate the view and its view holder, and another one to bind data to the view. The good thing about this is that the first method is called only when we really need to create a new view. No need to check if it's being recycled.
A solution these days instead of using 'subRecyclerViews' is to use one recyclerView with a ConcatAdapter
val firstAdapter: FirstAdapter
val secondAdapter: SecondAdapter
val thirdAdapter: ThirdAdapter
val concatAdapter = ConcatAdapter(firstAdapter, secondAdapter,
thirdAdapter)
recyclerView.adapter = concatAdapter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With