The Android Arsenal – View Adapters

The simple adapter to render static facts in RecyclerView.


Set up

Include 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 instances when you just need to have to render a basic record of static details devoid of the hassle. You never want to use elaborate options like Epoxy or Groupie for this objective. This task is ItemsAdapter was created for. ItemsAdapter presents you straightforward DSL to make adapters.

When ought to I not use ItemsAdapter?

  • When you have to have to update and re-attract details in adapter
  • When you need to render aspects with complicated logic

Use

The simplest case of use is:

recyclerView.adapter = itemsAdapter(areas)  area ->
    bind(R.structure.view_location)  // this: See
        view_location_title.textual content = region.title
        perspective_location_description.text = location.description
    

If you have much more than a single see kind, you can use operator when:

recyclerView.adapter = itemsAdapter(contactsItems)  product ->
    when (merchandise) 
        is ContactsItem.Header -> bind(R.structure.view_contacts_header) 
            look at_contacts_header_letter.textual content = product.letter
        

        is ContactsItem.Entry -> bind(R.layout.see_contacts_entry) 
            check out_contacts_entry_identify.text = product.title
            look at_contacts_entry_phone.text = item.cellphone
        
    

Notice that kotlin synthetics are deprecated because Kotlin 1.4.20, so it is much better to use ViewBinding or findViewById.

ViewBinding guidance

If you use ViewBinding, use itemsadapter-viewbinding in location of itemsadapter.
Then you can use approach bind with ViewBinding:

recyclerView.adapter = itemsAdapter(regions)  region ->
    bind<ViewRegionBinding>(R.structure.watch_location)  // this: ViewRegionBinding
        title.textual content = area.title
        description.text = area.description
    

Functions

Context

Within just itemsAdapter block you can use contextual facts:

Subject Description
index: Int Index of the recent merchandise

Looping method

Established parameter isLooping to true to use ItemsAdapter in looping method. It will only perform if you have far more than 1 component in details listing.

Contributing

Merge requests are welcome. For key improvements, remember to open an issue initially to examine what you would like to alter.

License

MIT

Next Post

Is Content Marketing the Star of Your RevOps Plan for 2023?

By 2023, it is believed that 25% of corporations will have a Chief Income Officer. Whilst a nod to generating profits expansion a precedence, the increase of this position is a testomony to analyzing and optimizing a company’s comprehensive revenue cycle to attain sustainable growth. Related Posts:The Android Arsenal - […]
Is Content Marketing the Star of Your RevOps Plan for 2023?

You May Like