ItemsAdapter

Additional

Language
Kotlin
Version
v1.1 (Feb 25, 2021)
Created
Nov 15, 2020
Updated
Jul 18, 2021 (Retired)
Owner
red_mad_robot (RedMadRobot)
Contributor
Osip Fatkullin (osipxd)
1
Activity
Badge
Generate
Download
Source code

ItemsAdapter

The simple adapter to render static data in RecyclerView.


Installation

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")
}

Rationale

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.

When should I not use ItemsAdapter?

  • When you need to update and re-draw data in adapter
  • When you need to render elements with complex logic

Usage

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 support

If 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
    }
}

Features

Context

Within itemsAdapter block you can use contextual data:

Field Description
index: Int Index of the current item

Looping mode

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.

Contributing

Merge requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT