Skip to content

Latest commit

 

History

History
76 lines (60 loc) · 1.96 KB

File metadata and controls

76 lines (60 loc) · 1.96 KB

BindingRvAdapter

Library to use RecyclerView without need to create ViewHolders, and with type safe way to connect with layouts.

It uses Android View Binding library to create ViewHolder that can later be filled inside lambda function in register method.

val adapter = BindingRVAdapter()
    .register<String, TestItemLayoutBinding>(TestItemLayoutBinding::inflate) { data ->
        title.text = data
        root.setOnClickListener {
            Toast.makeText(this@MainActivity, data, Toast.LENGTH_SHORT).show()
        }
    }

recyclerView.adapter = adapter

adapter.data = (0..2000).map { it.toString() }

How to start

  1. Step 1. Add the JitPack repository to your build file
allprojects {
    repositories {
        ...        
        maven { url 'https://jitpack.io' }
    }
}
  1. Step 2. Add the dependency
dependencies {
    implementation 'com.github.miszmaniac:bindingRvAdapter:2.0.0'
}
  1. Step 3. Enable data binding in your app module build.gradle file
// Enable kapt to generate binding classes
apply plugin: 'kotlin-kapt'

// Enable data binding
android {
    ...
    buildFeatures {
        viewBinding true
    }
}
  1. Step 4. Create adapter and start registering your data.

View binding generates classes with the same name as your Layout file: test_item_layout -> TestItemLayoutBinding

First generic type for register function is your Binding class and second is type of data that you want to assign with this view type.

val adapter = BindingRVAdapter()
    .register<String, TestItemLayoutBinding>(TestItemLayoutBinding::inflate) { data ->
        title.text = data
    }
    .register<Int, TestItemLayoutBinding>(TestItemLayoutBinding::inflate) { data ->
        title.text = context.getString(data)
    }
  1. Step 5. Update RecyclerView data
    adapter.data = listOf("Cat", "Dog", "Fish", R.string.bird)
  1. Step 6. This is it... just run your app now:)