Khalil Jomaa Khalil Jomaa - 16 days ago 7
Android Question

Design a RecyclerView with different layouts

I have a

RecyclerView
with different
CardView
layouts.

My question is that if I want to add dynamic list on each
CardView
, then should I add
ListView
on each card or add
RecyclerView
on each card?

An example will be appreciated.

Answer

To be honest it is not very common to add a RecyclerView inside another. It would be better for you to use a single RecyclerView and populate your list items dynamically. Here a github project to describe how this can be done https://github.com/comeondude/dynamic-recyclerview/wiki. I'd say have a look. There might be different approaches, this however, is a much faster and efficient way of showing multiple lists in a RecyclerView.

The idea is to add logic in your onCreateViewHolder and onBindViewHolder method so that you can inflate proper view for the exact positions in your RecyclerView.

There is a sample project along with that wiki too. You might clone and check what it does.

There is another way of keeping your items in a single ArrayList of objects so that you can set an attribute tagging the items to indicate which item is from first list and which one belongs to second list. Then pass that ArrayList into your RecyclerView and then implement the logic inside adapter to populate them dynamically. How to add a recyclerView inside another recyclerView

Tell me if this solved your problem ;)

Comments