The Android Arsenal – View Adapters

[ad_1]

The easy adapter to render static information in RecyclerView.


Set up

Add the dependency:

repositories {
    mavenCentral()
    google()
}

dependencies {
    implementation("com.redmadrobot.itemsadapter:itemsadapter:1.1")
    // or if you happen to use viewbinding
    implementation("com.redmadrobot.itemsadapter:itemsadapter-viewbinding:1.1")
}

Rationale

There are circumstances whenever you simply must render a easy listing of static information with out the effort. You do not need to use advanced options like Epoxy or Groupie for this goal. This process is ItemsAdapter was created for. ItemsAdapter gives you easy DSL to create adapters.

When ought to I not use ItemsAdapter?

  • When you must replace and re-draw information in adapter
  • When you must render components with advanced logic

Utilization

The only case of utilization is:

recyclerView.adapter = itemsAdapter(areas) { area ->
    bind(R.structure.view_region) { // this: View
        view_region_title.textual content = area.title
        view_region_description.textual content = area.description
    }
}

You probably have a couple of view kind, you should use operator when:

recyclerView.adapter = itemsAdapter(contactsItems) { merchandise ->
    when (merchandise) {
        is ContactsItem.Header -> bind(R.structure.view_contacts_header) {
            view_contacts_header_letter.textual content = merchandise.letter
        }

        is ContactsItem.Entry -> bind(R.structure.view_contacts_entry) {
            view_contacts_entry_name.textual content = merchandise.title
            view_contacts_entry_phone.textual content = merchandise.cellphone
        }
    }
}

Notice that kotlin synthetics are deprecated since Kotlin 1.4.20, so it’s higher to make use of ViewBinding or findViewById.

ViewBinding help

In the event you use ViewBinding, use itemsadapter-viewbinding instead of itemsadapter.
Then you should use methodology bind with ViewBinding:

recyclerView.adapter = itemsAdapter(areas) { area ->
    bind<ViewRegionBinding>(R.structure.view_region) { // this: ViewRegionBinding
        title.textual content = area.title
        description.textual content = area.description
    }
}

Options

Context

Inside itemsAdapter block you should use contextual information:

Discipline Description
index: Int Index of the present merchandise

Looping mode

Set parameter isLooping to true to make use of ItemsAdapter in looping mode. It can solely work when you have a couple of ingredient in information listing.

Contributing

Merge requests are welcome. For main modifications, please open a difficulty first to debate what you want to change.

License

MIT

[ad_2]

Source_link

Leave a Reply

Your email address will not be published.