New to Android development and Kotlin. I’m hoping to use different views based on the properties of my data class, but I’m not really sure how and I’m uncertain if what I want to do is even possible. I know I need to override getItemViewType, and leverage that in onCreateViewHolder, but I’m confused with the code for getItemViewType.
Room Data Class
data class PersonMessages(
@Embedded
val Person: Person,
@Relation(
parentColumn = "id",
entityColumn = "person_id"
)
val Messages: List<Messages>
)
RecyclerView Adapter
class PeopleViewAdapter: ListAdapter<PersonMessages, PeopleViewAdapter.ViewHolder>(PeopleDiffCallback()) {
// ...
override fun getItemViewType(position: Int): Int =
when (getItem(position)) {
is Messages -> R.layout.fragment_message_detail
is Person -> R.layout.fragment_people_detail
else -> throw IllegalStateException("Illegal item view type")
}
}
For getItemViewType, Android Studio correctly complains that Messages and Person are Incompatible types with PersonMessages, but I have no idea as to what I need to change, and where, to get this to work.
All clue sticks appreciated.