The alternative to switch
in Kotlin is when
. So, inside a recycler view adapter, when I am returning view type, I use when
:
Your when
is correct, however Kotlin has the ability to lift the return out of the 'when' if you are returning in every case, thus it becomes :
override fun getItemViewType(position: Int): Int {
return when (position) {
0 -> ItemViewType.TITLE.type
1 -> ItemViewType.SUBTITLE.type
2 -> ItemViewType.ITEM.type
else -> -1
}
}