I want to populate a list (RecycleView or similar) with any number of elements in runtime.
Example:
//Data Models
Animal(spices: String, breed: Breed)
Breed(name: String, color: String)
The list should look something like:
Cats
-----------
Ragdoll
White
Bengal
Beige/Black
Dogs
-----------
Golden Retriever
Beige
German Shepard
Brown
St. Bernard
White/Brown
The list can be infinite long and each Animal can have an infinite amount of Breeds.
I have been using nested recyclers but I am afraid this will cause bad performance.
What is the "correct" way of populating this kind of list?
You'll need a RecyclerView Adapter with multiple wiew types. Check out this codelab, it should help you out.
Edit: the easiest way to host all this data in the adapter is to flatten it. Kind of like:
val data = mutableListOf<Object>()
animals.forEach { animal ->
data.add(animal)
animal.breeds.forEach { -> breed
data.add(breed)
}
}
// ... use the data as a source for your adapter
The item view type
override fun getItemViewType(position: Int) =
when (getItem(position)) {
is Animal -> ITEM_VIEW_TYPE_HEADER
is Breed -> ITEM_VIEW_TYPE_ITEM
}
etc etc