Building a Reactive & Heterogeneous Adapter in Kotlin

Best way to manage multiple view types even with nested recyclerviews

Siva Ganesh Kantamani
Programming Geeks

--

At the end of this article, I’ve provided a public GitHub sample, feel free to play with it.

If you’re in android development for a while, you might have faced a situation where you need to implement multiple view-types in the recyclerview. The very common way you might have encountered this situation is while implementing paging.

Conventional way

While implementing paging you need to implement loading at the end of the list while executing a service call and the very common way to do this is using view types.

class ItemViewHolder extends RecyclerView.ViewHolder{

}
class LoadingViewHolder extends RecyclerView.ViewHolder{

}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return if (viewType == LOADING) {
LoadingViewHolder(mLayoutInflater.inflate(R.layout.loading, parent, false))
} else {
ItemViewHolder(mLayoutInflater.inflate(R.layout.item_layout, parent, false))
}
}

--

--