Android RecyclerView With Kotlin Sealed Classes

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.

Taking inspiration from that article, I want to implement view types in RecyclerView using sealed classes. We will try to move the comparison of random integers or layouts to class types. If you’re a Kotlin nerd, I’m sure you’ll like this implementation.

Create the Kotlin Sealed Class

The first thing we need to do in this approach is to create all the data classes we wish to use in the adapter and then link them in a sealed class. Let’s create a bunch of data classes:

data class FeedItem(val title: String,
                    val desp : String,
                    val businessName : String,
                    ...)

data class PromotionItem(val title: String,
                         val desp : String,
                         val image : String)

data class RatingCardItem(val title: String,
                         val desp : String,
                         val link : String,
                         val button_tittle : String)

data class LoadingStateItem(val isLoading: Boolean,
                          val isRetry : Boolean,
                          val error_message : String)

kotlin android

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Top Android Projects with Source Code

Android projects with source code - Work on real-time android projects. We’ll start project ideas from beginners level and later move to advance projects.

How to Develop an Android App with Kotlin?

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

Top Kotlin Android App Development Company in USA

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

Top-notch Kotlin App Development Company in USA

AppClues Infotech is a top Mobile App Development Company in USA building high-quality Android, iOS, and Native apps for Startups, SMBs, & Enterprises. Contact us now!

Kotlin Coroutines on Android - How to use Coroutines on Android

Coroutines are a Kotlin feature that convert async callbacks for long-running tasks, such as database or network access, into sequential code. This Kotlin Coroutines tutorial will show you how to use coroutines on Android, and how the new androidx-concurrent library makes it easy to use them to get things off the main thread. You'll also learn how the new library helps coroutines work with Architecture Components. This session also covers coroutine patterns, best practices, and even how to test coroutines!