Dick Lucas
Dick Lucas

Reputation: 12639

Variable number of RecyclerView items in multiple CardViews

I have a RecyclerView whose items must be grouped in CardViews. There are a variable number of items that will be grouped in each CardView and the item ViewTypes differ as well. I am struggling with how I can add items to each CardView such that it grows based on the number of items.

I have attempted using nested RecyclerViews but this has proved buggy.

All help is greatly appreciated. Thank you.

Upvotes: 0

Views: 314

Answers (1)

Dick Lucas
Dick Lucas

Reputation: 12639

I have attempted using nested RecyclerViews but this has proved buggy.

My implementation is what was buggy. The solution I settled on was to use nested RecyclerViews. A top level RecyclerView that would hold the CardViews and a RecyclerView in each CardView to display the variable number of items.

The reason my original implementation was "buggy" was because if the nested RecyclerView had no items to display, I set the Visibility to GONE. This was a poor attempt at optimization. This would cause issues where an item would dynamically be added to the nested RecyclerView but it wouldn't appear because the nest RecyclerView was set to GONE.

If you are using nested RecyclerViews, in your onBindViewHolder() method in the Adapter for the top level RecyclerView, be sure to update the items in the adapter of the nested RecyclerView or else it will contain recycled data meant for a different position.

Upvotes: 1

Related Questions