gitmyhub

MultiType

Kotlin ★ 5.8k updated 3y ago

Flexible multiple types for Android RecyclerView.

MultiType is an Android library that makes it easy to build scrolling lists with many different visual styles. Each item type gets its own small class, so adding a new look never breaks existing code.

KotlinAndroidRecyclerViewAndroidXsetup: easycomplexity 2/5

MultiType is an Android library written in Kotlin that makes it easier to build scrolling lists where different items in the list look different from each other. On Android, scrolling lists are typically built using a component called RecyclerView. By default, adding multiple visual styles to one list requires a fair amount of repetitive, error-prone code that becomes harder to change over time. MultiType is designed to remove that friction.

The core idea is to separate each visual style into its own small class, called a delegate. Instead of one large adapter class that knows about every item type in the list, you register individual delegates with the adapter, one per type. When the list needs to display a particular kind of item, the adapter forwards that item to the matching delegate, which knows how to draw it. Adding a new item type means creating a new delegate class and registering it; none of the existing code needs to change.

The library also supports a one-to-many mapping, where a single data type can be displayed in more than one way depending on the content of the item. A linker function inspects each item and decides which delegate should handle it. This handles cases like a feed where items of the same category should render differently based on some flag or value inside the data.

An Android Studio plugin is available separately that generates the boilerplate files for a new item type automatically, reducing the setup work further.

MultiType is licensed under the Apache 2.0 license. It requires AndroidX and is written fully in Kotlin from version 4 onwards. A legacy Java-compatible version (3.x) is available for projects that have not migrated.

Where it fits