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.