Rekata Adapter

Additional

Language
Kotlin
Version
N/A
Created
Jun 8, 2019
Updated
Sep 3, 2019
Owner
Gian Patrick Quintana (HirogaKatageri)
Contributor
Gian Patrick Quintana (HirogaKatageri)
1
Activity
Badge
Generate
Download
Source code
APK file

Promotion

Rekata Adapter

Recycler View Adapter that supports multiple views.

Benefits

  • No need for ViewHolders.
  • Easily bind Item Views and their behavior.
  • Easily modify items that share common ids. (e.g. Whether a user is following someone or not)
  • Easy event handling.
  • Loosely coupled.

Examples

A simple setup of the Adapter

    private val config: AdapterConfig = AdapterConfig(true).apply {
        addViewType(DemoViewTypes.TYPE_IMAGE, R.layout.item_sample_image)
        addViewType(DemoViewTypes.TYPE_TEXT, R.layout.item_sample_text)
    }

    private val adapter: SimpleRekataAdapter = SimpleRekataAdapter(config)

A simple Item for the Adapter

class SimpleTextItem(position: Int) : SimpleRekataItem(
    viewType = DemoViewTypes.TYPE_TEXT,
    itemId = position.toLong(),
    sharedId = position.toString(),
    priority = 1
) {
    override fun bindItem() {
        txt_sample.text = "Position ${holder.adapterPosition}"
        containerView.setOnClickListener { trigger(TextClickEvent(holder.adapterPosition)) }
    }
}

Event Handling

  1. Create a class that inherits RekataEvent. Note: You can also pass data in the class.
  2. Add something like an onClickListener to a view and call trigger() inside the item class.
  3. You can add event listeners in the adapter by calling addEventListener().

Creating Events

class TextClickEvent(val position: Int) : RekataEvent

Triggering Events inside an Item

class SimpleTextItem(position: Int) : SimpleRekataItem(
    viewType = DemoViewTypes.TYPE_TEXT,
    itemId = position.toLong(),
    sharedId = position.toString(),
    priority = 1
) {

    override fun <T : IRekataItem> bindItem(item: T) {
        txt_sample.text = "Position ${holder.adapterPosition}"
        holder.itemView.setOnClickListener { trigger(TextClickEvent(holder.adapterPosition)) }
    }
}

Adding Event Listeners

    private val adapter: SimpleRekataAdapter = SimpleRekataAdapter(config).apply {
        addEventListener<TextClickEvent, SimpleTextItem> { event, item ->
            Toast.makeText(
                item.holder.itemView.context,
                "You've clicked a simple text at position: ${event.position}",
                Toast.LENGTH_SHORT
            ).show()
        }
    }

Gradle Setup

Project Gradle

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

App Gradle

dependencies {
    implementation 'com.github.HirogaKatageri:rekata-adapter:TAG'
}