-3

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.

adarsh
  • 6,738
  • 4
  • 30
  • 52

2 Answers2

1

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 ;)

Community
  • 1
  • 1
Cédric Portmann
  • 1,014
  • 1
  • 9
  • 22
0

in .onCreateViewHolder() method you receive int variable viewType Use it for distinguishing between different layout types

Alex Shutov
  • 3,217
  • 2
  • 13
  • 11