Search code examples
androidandroid-recyclerviewandroid-nestedscrollview

Why am I experiencing ANRs with RecyclerView inside NestedScrollView and what are the recommended alternatives?


We have implemented a RecyclerView inside a NestedScrollView in multiple places within our app. This is a common practice we have followed, especially on screens where a large list of items needs to be displayed. However, we are experiencing multiple ANR (Application Not Responding) errors related to the RecyclerView. The most frequently reported ANR trace is as follows:

androidx.recyclerview.widget.RecyclerView.access$000 (RecyclerView.java:212)
androidx.recyclerview.widget.RecyclerView$5.attachViewToParent (RecyclerView.java:960)
androidx.recyclerview.widget.ChildHelper.attachViewToParent (ChildHelper.java:239)
androidx.recyclerview.widget.RecyclerView$LayoutManager.addViewInt (RecyclerView.java:8883)
androidx.recyclerview.widget.RecyclerView$LayoutManager.addView (RecyclerView.java:8860)

When analyzing the layout using the layout inspector, we noticed that all items are being drawn at once, which seems to be a contributing factor to the performance issues.

We have come across various blog posts and articles advocating this pattern, while others discourage it. Could this implementation of a RecyclerView inside a NestedScrollView be a potential cause of the ANRs we are experiencing? If so, what are the recommended alternatives or best practices to optimize the performance in this scenario?

Thank you!

Removing the usage of nested scroll view


Solution

  • Using recyclerview inside NestedScrollView generally not recommended as a good practice. Placing a RecyclerView inside a NestedScrollView can negatively impact performance. The RecyclerView is designed to efficiently handle large datasets by recycling and reusing views as needed. when placed inside a NestedScrollView, all RecyclerView items are rendered at once, regardless of their visibility on the screen due to this increase memory usage and NestedScrollView can cause scrolling issues if not used correctly and device produce ANR.