Danny Preussler
1 min readSep 23, 2016

--

yes if you have more than one Viewholder you can’t go without an if/switch. Something like:

fun forType(parent: ViewGroup, viewType: Int) : ViewHolder {
when(viewType) {
in listOf(R.layout.duck, R.layout.dog, R.layout.mouse) -> return AnimalViewHolder(parent)
else -> return CarViewHolder(parent)
}
}

For me this function should be in the same class that knows all the types, like the visitor implementation. So it becomes like a factory. This way you don’t split all the types all over the place.

--

--

Danny Preussler
Danny Preussler

Written by Danny Preussler

Android @ Soundcloud, Google Developer Expert, Goth, Geek, writing about the daily crazy things in developer life with #Android and #Kotlin

Responses (1)