Supporting different Item types
In the previous sections, we learned how to handle a list of items of a single type (in our case, all our items were CatUiModel
). What happens if you want to support more than one type of item? A good example of this would be having group titles on our list.
Let’s say that instead of getting a list of cats, we get a list containing happy cats and sad cats. Each of the two groups of cats is preceded by a title of the corresponding group. Instead of a list of CatUiModel
instances, our list would now contain ListItem
instances. ListItem
might look like this:
sealed class ListItem { data class Group(val name: String) : ListItem() data class Cat(val cat: CatUiModel) : ListItem() }
Our list of items may look like this:
listOf( ListItem.Group("Happy Cats"), ListItem.Cat( CatUiModel(Gender.Female, CatBreed.AmericanCurl, ...