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() }
- Step 1. Add the JitPack repository to your build file
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- Step 2. Add the dependency
dependencies {
implementation 'com.github.miszmaniac:bindingRvAdapter:2.0.0'
}
- 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
}
}
- 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)
}
- Step 5. Update RecyclerView data
adapter.data = listOf("Cat", "Dog", "Fish", R.string.bird)
- Step 6. This is it... just run your app now:)