The simple adapter to render static data in RecyclerView
.
Add the dependency:
repositories {
mavenCentral()
google()
}
dependencies {
implementation("com.redmadrobot.itemsadapter:itemsadapter:1.1")
// or if you use viewbinding
implementation("com.redmadrobot.itemsadapter:itemsadapter-viewbinding:1.1")
}
There are cases when you just need to render a simple list of static data without the hassle.
You don't want to use complex solutions like Epoxy or Groupie for this purpose.
This task is ItemsAdapter
was created for.
ItemsAdapter
offers you simple DSL to create adapters.
ItemsAdapter
?The simplest case of usage is:
recyclerView.adapter = itemsAdapter(regions) { region ->
bind(R.layout.view_region) { // this: View
view_region_title.text = region.title
view_region_description.text = region.description
}
}
If you have more than one view type, you can use operator when
:
recyclerView.adapter = itemsAdapter(contactsItems) { item ->
when (item) {
is ContactsItem.Header -> bind(R.layout.view_contacts_header) {
view_contacts_header_letter.text = item.letter
}
is ContactsItem.Entry -> bind(R.layout.view_contacts_entry) {
view_contacts_entry_name.text = item.name
view_contacts_entry_phone.text = item.phone
}
}
}
❗ Note that kotlin synthetics are deprecated since Kotlin 1.4.20, so it is better to use ViewBinding or
findViewById
.
ViewBinding
supportIf you use ViewBinding
, use itemsadapter-viewbinding
in place of itemsadapter
.
Then you can use method bind
with ViewBinding
:
recyclerView.adapter = itemsAdapter(regions) { region ->
bind<ViewRegionBinding>(R.layout.view_region) { // this: ViewRegionBinding
title.text = region.title
description.text = region.description
}
}
Within itemsAdapter
block you can use contextual data:
Field | Description |
---|---|
index: Int | Index of the current item |
Set parameter isLooping
to true
to use ItemsAdapter
in looping mode.
It will only work if you have more than one element in data list.
Merge requests are welcome. For major changes, please open an issue first to discuss what you would like to change.