-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
How to Build Android Apps with Kotlin - Second Edition
By :
In the previous sections, we learned how to handle a list of items of a single type (in our case, all our items were EmployeeUiModel). 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 employees, we get a list containing current and past employees. Each of the two groups of employees is preceded by the title of the corresponding group. Instead of a list of EmployeeUiModel instances, our list would now contain ListItemUiModel instances. ListItemUiModel might look like this:
sealed interface ListItemUiModel {
data class GroupTitle(val name: String) : ListItemUiModel
data class Employee(
val employee: EmployeeUiModel
) : ListItemUiModel
We wrap the two object types in a sealed interface for compile-time type safety. A list containing only objects of the GroupTitle and Employee types can be...