Skip to content Skip to sidebar Skip to footer

Why Does My Staggeredgrid Recyclerview Layout Every Item, Not Only Visible Ones?

I have a Staggered grid containing 370 items, with images. I want to make sure the items are recycled quickly to be careful with memory, but a ViewHolder is created and then bound

Solution 1:

Problem:

The reason you are facing this is issue is because you have added RecyclerView in NestedScrollView.

Reason:

It's not first time I have heard of this issue, me and probably everyone who has tried to put RecyclerView in NestedScrollView has faced this issue (if noticed).

As far as I could figure the reason, it is because when you place RecyclerView in NestedScrollView, it is unable to identify exact height required for RecyclerView. What normally a developer assumes for this (in simple words) is RecyclerView height should be match_parent once all above views have gone off screen. But unfortunately, this is NOT the case.

It makes RecyclerView somehow wrap_content adding all its views and then measuring its height (correct me if I am wrong). Not sure a possible bug or expected behaviour, but I believe NestedScrollView should be able to handle this case explicitly, otherwise, adding RecyclerView in NestedScrollView is completely useless, as it does not recycle views, completely destroying the RecyclerView concept and thus consuming a lot of memory.

Temporary Answer :

Just remove the RecyclerView from NestedScrollView so that it can properly reuse the views.

NOTE: The answer may not be 100% right as it is based completely on my personal observation and experience. Any better solution or improvements in the answer is appreciated.

Solution 2:

The issue is in NestedScrollview the space available to the recyclerview is not determined.

You can use android:fillViewport="true" to make NestedScrollView measure the RecyclerView. The RecyclerView will fill the remaining height. so if you want to scroll the NestScrollView, you can set the RecyclerView's minHeight.

Post a Comment for "Why Does My Staggeredgrid Recyclerview Layout Every Item, Not Only Visible Ones?"