Search code examples
javaandroidkotlinandroid-recyclerviewandroid-coordinatorlayout

RecyclerView's performance benefits get stripped when it is inside a NestedScrollView. Is there a workaround to use both?


When I put my RecyclerView inside a NestedScrollView. The LayoutManager and RecyclerView return the same values for getChildCount() and getItemCount() (they are ALWAYS the same, and ALWAYS the max value of the ArrayList/MutableList).

I have a large dataset added to the RecyclerView's custom adapter. Say that it is 400 image thumbnails. getItemCount() should return 400, while getChildCount() should only return like ~20 image thumbnails that a RecyclerView can fit on a screen.

When I use RecyclerView under the NestedScrollView, the getChildCount() always returns 400. I think my RecyclerView is not recycling views like it is supposed to and that I actually have 400 thumbnails displayed at once, instead of 20.

Now, I know I should just use RecyclerView by itself. Most likely, the removal of the NestedScrollView will make RecyclerView work the way it is supposed to. But I need to implement the collapsing toolbar which is typically done with a NestedScrollView with the CoordinatorLayout.

Are there any workarounds to have a RecyclerView with a collapsing toolbar?

My implementation is similar to this.


Solution

  • It's working as it's intended to be. This means When you add a ReyclerView inside a NestedScrollView the RecyclerView will not recycler views instead it will create all the views. Since you have a large set of 400 items it is bad to add recycler view inside NestedScrollView. It will cause serious performance issues when scrolling down.

    If you want to implement Collapsing Toolbar, you can still achieve the same with RecyclerView. I don't how your layout looks like. However, you can refer to this article to see how it can be done