I saw an interesting viewholder implementation in this tweet https://twitter.com/AndroidDev/status/972502799496790018
override fun onBindViewHolder(holder: SealedAdapterViewHolder, position: Int) {
return when (holder) {
is HeaderHolder -> holder.displayHeader(items[position])
is DetailsHolder -> holder.displayDetails(items[position])
}
}
Unfortunately i can't figure out how to implement thouse holders. And I didn't find any examples of this trick.
In my viewholders I have to extend RecyclerView.ViewHolder and I have to extend sealed class so i can use it in "when clauses". Multiple inharitance is not allowed.
So is it possible and if it is then how?
P.S.
original authors write this:
You can also use sealed classes in a RecyclerView adapter. They’re a perfect fit for ViewHolders - with a clean set of types to dispatch explicitly to each holder. Used as an expression, the compiler will error if all types aren’t matched.
Something like this:
it's in the first post of the thread.