A simple recyclerView adapter using ListAdapter, DataBinding and DiffUtil
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.SongDanielLi:SongAdapterLib:Tag'
}
class CardRow(override val value: String): BindingItem<CardViewBinding, String>() {
override val viewType: Int
get() = R.layout.card_view
override fun createBinding(inflater: LayoutInflater, parent: ViewGroup, viewType: Int): CardViewBinding {
return CardViewBinding.inflate(inflater, parent, false)
}
override fun onBind(binding: CardViewBinding) {
binding.txtCard.text = value
}
// implement onClick listener
override fun onClickListener(view: View, position: Int) {
Toast.makeText(view.context, "Click: $position", Toast.LENGTH_LONG).show()
}
}
val adapter = BindingAdapter()
recyclerView.adapter = adapter
// create item
val row = CardRow("Test")
// add to adapter
adapter.submitList(listOf(row))
Android Kotlin Fundamentals: DiffUtil and data binding with RecyclerView