RecyclerView with NestedScrollView best practices and how to avoid it

RecyclerView forms a very crucial part of the UI in Android App development. It is especially important to optimize memory consumption during the display of a long list of items. A RecylerView inflates a customized list of items. This list can have either all similar layouts or multiple distinct layouts.

To use recyclerView we have four patterns
1- RecyclerView with a single view type
2- RecyclerView with Multiple ViewType
3- RecyclerView inside NestedScrollView “bad performance
4- RecyclerView with Fragment type

The first pattern is the most common and the second pattern will not be covered in this article as there are a lot of articles that cover it; you can search and find it easily.

In this article, we will talk about the third and fourth patterns. Get ready

RecyclerView inside NestedScrollView

RecyclerView inside NestedScrollView is needed when there is a layout that contains some views and a small space for RecyclerView, so all views have to be scrolled to see all RecyclerView items.

Using this pattern, the recycler pattern doesn’t work. all the views will be loaded at once because wrap_content needs the height of complete RecyclerView so it will draw all child Views at once. No view will be recycled and as a result the performance will be so bad

When can I use this pattern?

Try not to use this pattern unless there are few items in the list, maybe less than eight. The fourth pattern will be the solution if the list has many items. The performance impact will be very high.

How can I use this pattern?

1- In XML layout, we need to put the RecyclerView inside NestedScrollView, with `android:layout_height=”wrap_content”`

2- As RecyclerView needs to set LayoutManager and setAdapter

3- `recyclerView.setHasFixedSize(false)

Here setHasFixedSize(false) determines that changes in adapter content can change the size of the RecyclerView.

4- `recyclerView.setNestedScrollingEnabled(false)

Here setNestedScrollingEnabled(false) disable scrolling for RecyclerView, so it doesn't intercept scrolling event from NestedScrollView.

Now everything is set! you can test your app.

RecyclerView with Fragment type

The fourth pattern to use RecyclerView is so helpful when you have a complex UI above RecyclerView, we can use the second pattern, but then we should handle too many item types, therefore using this pattern will be easier especially if we need to use that complex UI in another place.

Using this pattern, the recycler pattern will work. Just visible views will be loaded and the fragment will be treated as a RecyclerView item.

The strategy for this pattern is to separate the complex UI and the RecyclerView, the complex UI with NestedScrollView will be separated as a new fragment and then we will use two item types for RecyclerView; the first one for fragment and the second one for the normal item. Then display the fragment in the first item.
In this case, we will treat the RecyclerView fragment item as a holder for our fragment.

When can I use this pattern?

I recommend using this pattern in two cases:
1- When you have a complex UI above RecyclerView.
2- When you need to use the UI in another place.

How can I use this pattern?

1- Create a new Fragment and move complex UI into it.

2- Add a new item to ArrayList in position zero.

3- Create an Interface to use as a callback for calling display Fragment in MainActivity.

4- Replace the first RecyclerView item with our Fragment.

5- In the Adapter create two item types and call the Interface’s method in Fragment type.

Now we have a RecyclerView with a complex UI on the same screen without using RecyclerView inside NestedScrollView.

Next Steps

The full code sample can be found here.

Thank you for reading my article about RecyclerView.

Don’t hesitate to contact me if you have any questions or concerns.

Expert Android Developer | Software Engineering