7

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?

Narendra Sorathiya
  • 3,770
  • 2
  • 34
  • 37
Mischmo
  • 75
  • 2
  • 7
  • 1
    Using recyclerViews insde of other recyclerView doesn't seem like a good idea to me. You end up with performance issues (like this lag you're having). Remember that you can also display lists using `ListView`s or enable scrolling using `ScrollView`s. – Rosário Pereira Fernandes Feb 03 '17 at 19:48
  • @RosárioPereiraFernandes Could you explain, why using `ListView` would be better than using `RecyclerView`? I always thought, that they added `RecyclerView` because it was better optimized than `ListView`. Using `ScrollView` might be a better way but there is a problem that I don't know how many SubRecyclerViews are there gonna be until I receive data from server. Also each of the SubRecyclerViews can display data in different way, some of them are grids, some of them horizontal lists or the usual vertical lists. This is based on the data from server as well. – Mischmo Feb 06 '17 at 09:53
  • Oh, I see.. Well, the main difference between RecyclerView and ListView is that ListViews create all of it's views when you set the Adapter. But RecyclerViews only create the visible Views when you set the Adapter; When those views are no longer visible, they are destroyed and the "new visibles" are created. That's why I think having subRecyclerViews might be a problem: you'll be creating and destroying many views simultaneously. – Rosário Pereira Fernandes Feb 06 '17 at 10:03
  • @RosárioPereiraFernandes I think I know what you mean but since my `RecyclerView` is creating all the Views at once, isn't that just like using the `ListView`? Also I don't think that `RecyclerView` is exactly destroying old Views and creating new ones for newly visible items. In my understanding it only recycles the old Views and reuses them for the new items. And since the lag only occurs when a new SubRecyclerView comes into view and onCreateViewHolder() method is called for all the SubRecyclerView items at once, I think the problem is exactly that it behaves like `ListView`. – Mischmo Feb 06 '17 at 10:52
  • @Mischmo: Were you ever able to figure this out? – Kevin Dec 12 '17 at 08:23
  • @Kevin: Unfortunately I haven't been able to figure it out. If you ever manage to find a solution, please post it here, it would be much appreciated. – Mischmo Jan 12 '18 at 10:35

1 Answers1

1

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
MSpeed
  • 8,153
  • 7
  • 49
  • 61