Member-only story
Android RecyclerView With Kotlin Sealed Classes
Multiple view types in RecyclerViews using sealed classes
One of the best ways to display huge lists in Android is through RecyclerView
. As developers, you all might have used it. We have many advanced features like view holder patterns, rich animation, Diff-Utils callback to improve performance, etc. Apps like WhatsApp and Gmail use RecyclerView
to show endless conversations.
One of the significant RecyclerView
features that I use is view types. We can display multiple view types in the same RecyclerView
. In the early days, developers used to do this by maintaining a view type flag in the list model and returning it in the getViewType
function of the RecyclerView
adapter.
Why Kotlin Sealed Classes?
After the introduction of Kotlin for Android development, how we approach core implementations changed drastically. I mean, features like extensions almost replaced the need to maintain base classes for Android components. Kotlin delegates changed how we work with setters and getters.
Now, it’s time to update how we do things in a RecyclerView
adapter for the best. Kotlin sealed classes show a significant effect on state management. If you want to learn about them, I suggest you read this article.