Search code examples
androidoptimizationandroid-recyclerviewnestedrecyclerview

RecyclerView in a RecyclerView item


I know it's a bad idea, but I'm using a RecyclerView inside another RecyclerView's item to display a list of items containing a list.

I managed to get it work and tweaked it as far as I could, but I still get those green spikes when lists with a bigger number of items begin to show and are being drawn on the screen.

GPU profiling Samsung Galaxy S6

Those spikes produce a noticeable lag when flinging through items and are even more obvious when using something slower and older than Samsung Galaxy S6.

I did some logging, and it appears that the outer ViewHolders are being recycled and the inner RecyclerView's adapter is created only 5 times so it must be the drawing time that kills the performance.

I've managed to limit the creation of new inner RecyclerView's ViewHolders by setting the maximum number of ViewHolders in the RecycledViewPool but that helped only a little.

I know that the inner RecyclerView doesn't have a "bottom" on which it could rely to calculate when to show new items so it has to draw every item immediately and that is probably the main reason why one shouldn't use this setup. (I come to my senses as I write this question.. thank you rubber ducks of the world).

Is there a way this could be tweaked even further or could you suggest how to make this work using some different setup other than RecyclerView inside another RecyclerView's item?

I will provide more info if needed.

Thank you


Solution

  • I managed to get better performance by creating a shared RecycledViewPool, as it was done in a project @Raghunandan linked in the comments.

    Google I/O Android App - Shared RecycledViewsPool

    I also set the max number of views to that Pool which really did the trick, now I just have to find a way to reuse my listeners and avoid creating new ones, but that's a different issue.

    Thank you all for responding. I will post more if I find anything useful in the future.