https://kotlinlang.org logo
#android
Title
# android
r

Rok Koncina

04/15/2019, 9:21 AM
Hello, I need an advice. I'm trying to implement a clean RecyclerView Adapter for multiple view types. This is as close as I can get but I can't get rid of that @Suppress annotation (or a Lint warning). Do you have any suggestion on how to clean this up even more?
g

gildor

04/15/2019, 9:26 AM
You cannot really get rid of it without suppression, because RecyclerView adapter in your case has in and out variance. You can for example check Kotlin Collection interface declaration that has
out
variance, but because also has methods like containsAll which require
in
variance suppress it
See more info about generics here https://kotlinlang.org/docs/reference/generics.html
there are also a few nice articles about this topic
r

Rok Koncina

04/15/2019, 9:38 AM
Yeah, I read that and came to the came conclusion. I was hoping I missed some detail or that somebody knows of a nice workaround. 😉
d

Daniel Garibaldi

04/15/2019, 9:42 AM
FirstViewHolder and SecondViewHolder should extend SomeBaseViewHolder and you dont need the cast
g

gildor

04/15/2019, 9:43 AM
FirstViewHolder and SecondViewHolder already extend SomeBaseViewHolder
👍 1
Problem not with it, but with type T of SomeBaseViewHolder
d

Daniel Garibaldi

04/15/2019, 9:44 AM
ahh yes, didnt see the code of the viewholders sorry
3 Views