我想在运行时中用任意数量的元素填充列表(RecycleView或类似内容)。
示例:
//Data Models
Animal(spices: String, breed: Breed)
Breed(name: String, color: String)
列表应类似于:
Cats
-----------
Ragdoll
White
Bengal
Beige/Black
Dogs
-----------
Golden Retriever
Beige
German Shepard
Brown
St. Bernard
White/Brown
列表可以无限长,每个动物可以有无限数量的品种。
我一直在使用嵌套式回收站,但恐怕这会导致性能下降。
填充这种列表的“正确”方法是什么?
答案 0 :(得分:1)
您将需要具有多种wiew类型的RecyclerView适配器。查看this codelab,它可以为您提供帮助。
编辑:将所有这些数据托管在适配器中的最简单方法是将其展平。有点像:
SimpleStateShape
项目视图类型
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
等等等