AdapterDsl is an Android library that simplifies the process of creating and managing RecyclerView
adapters using a Domain-Specific Language (DSL). It provides a concise and intuitive way to define
multiple item types, handle data binding, and manage pagination.
AdapterDsl uses a DSL to configure RecyclerView adapters, making the code more readable and
maintainable. You can define item types, view bindings, and data payloads in a single, coherent
block.
fun createMyAdapter() = createAdapter<Item> {
item(Item.Title::class, ItemTitleBinding::inflate) {
key { it.id }
span = AdapterItem.Span.Full
payload(Item.Title::text) {
binding.title.text = data.text
}
}
item(Item.Content::class, ItemContentBinding::inflate) {
key { it.id }
payload(Item.Content::text) {
binding.title.text = data.text
}
}
}Easily support multiple item types in a single RecyclerView adapter. Each item type can have its
own view binding, data binding logic, and span configuration.
AdapterDsl supports data payloads, which allow for partial updates of views when only a portion of the data has changed. This can significantly improve performance by reducing unnecessary view redraws.
The library provides built-in support for pagination using PagingDataAdapter. You can create a
paging adapter with the same DSL configuration as a regular adapter.
fun createMyPagingAdapter() = createPagingAdapter<Item> {
item(Item.Title::class, ItemTitleBinding::inflate) {
key { it.id }
span = AdapterItem.Span.Full
payload(Item.Title::text) {
binding.title.text = data.text
}
}
item(Item.Content::class, ItemContentBinding::inflate) {
key { it.id }
payload(Item.Content::text) {
binding.title.text = data.text
}
}
}Configure the span size of each item type, making it easy to create grid layouts with different span counts for different item types.
item(Item.Title::class, ItemTitleBinding::inflate) {
key { it.id }
span = AdapterItem.Span.Full
// ...
}AdapterDsl is available on Maven. To use it in your project, add the following dependency to your
build.gradle file:
implementation 'top.cyclops:adapter-dsl:x.y.z'Replace x.y.z with the latest version of AdapterDsl.
First, define your data models as sealed interfaces or classes.
sealed interface Item {
val id: Int
data class Title(override val id: Int, val text: String) : Item
data class Content(override val id: Int, val text: String) : Item
}Use the createAdapter or createPagingAdapter function to create an adapter with the DSL
configuration.
val adapter = createMyAdapter()Set the adapter to a RecyclerView and configure the layout manager.
val layoutManager = GridLayoutManager(this, 8)
binding.list.layoutManager = layoutManager
adapter.bindSpanLookup(layoutManager)
binding.list.adapter = adapterIf you are using a paging adapter, submit the PagingData to the adapter.
lifecycleScope.launch {
pager.flow.collectLatest {
adapter.submitData(it)
}
}Contributions are welcome! If you find a bug or have a feature request, please open an issue on the GitHub repository. If you want to contribute code, please fork the repository and submit a pull request.
AdapterDsl is licensed under the Apache License 2.0. See the LICENSE file for details.