Recycler Adapter - Krunal-Kevadiya/kotlin-common GitHub Wiki
kotlin dsl for recycler adapter to simplify RecyclerView.Adapter logic
A kotlin dsl mechanism to simplify and reduce boilerplate logic of a RecyclerView.Adapter.
With SingleAdapter or MultiTypedAdapter you can use all power of kotlin dsl to avoid wired standard implementation of RecyclerView.Adapter view types. You can easily maintain, update, move, swap, remove or add new view types using dsl code blocks and also provider normal or with DataBinding Recycerview adapter.
Bonus: Almost all logic by default works with DiffUtil
- Simple adapter without view types and DataBinding:
val adapter = recyclerView.setUpBinding<String> {
withLayoutResId(R.layout.item_advertisement)
onBind { index, item ->
setBackgroundColor(getRandomColor())
textViewAdvertisement.text = it
}
onClick { id, index, item ->
logs("(${R.id.textViewAdvertisement}, $id) -> $item", LogType.ERROR)
}
withItems(mutableListOf("one", "two", "three", "four", "five", "six", "seven"))
}- Simple adapter without view types but allow DataBinding:
val adapter = recyclerView.setUpBinding<String> {
withLayoutResId(R.layout.item_advertisement)
onBind<ItemAdvertisementBinding>(BR.viewModel) { index, item ->
this.textViewAdvertisement.setTextColor(Color.BLUE)
}
onClick(R.id.textViewAdvertisement) { id, index, item ->
logs("(${R.id.textViewAdvertisement}, $id) -> $item", LogType.ERROR)
}
withItems(mutableListOf("one", "two", "three", "four", "five", "six", "seven"))
}- Simple adapter with view types but without DataBinding:
adapter = recyclerView.setUpBinding {
withDiffUtils { false }
withContentComparator { s1, s2 -> s1 == s2 }
withItemsComparator { s1, s2 -> s1.hashCode() == s2.hashCode()}
withViewType<String> {
withLayoutResId(R.layout.item_advertisement)
onBind { index, item ->
textViewAdvertisement?.text = it
}
onClick(R.id.textViewAdvertisement) { id, index, item ->
logs("(${R.id.textViewAdvertisement}, $id) -> $item", LogType.ERROR)
}
}
withViewType<Int> {
withLayoutResId(R.layout.item_loadmore)
onBind { index, item ->
textViewLoadMore?.text = it.toString()
}
onClick(R.id.textViewLoadMore) { id, index, item ->
logs("(${R.id.textViewLoadMore}, $id) -> $item", LogType.ERROR)
}
}
withItems(mutableListOf("one", 1, "two", 2))
}- Simple adapter with view types and DataBinding:
adapter = recyclerView.setUpBinding {
withViewType<String> {
withLayoutResId(R.layout.item_advertisement)
onBind<ItemAdvertisementBinding>(BR.viewModel) { index, item ->
this.textViewAdvertisement.text = item
}
onClick { id, index, item ->
logs("(${R.id.textViewAdvertisement}, $id) -> $item", LogType.ERROR)
}
}
withViewType<Int> {
withLayoutResId(R.layout.item_loadmore)
onBind<ItemLoadmoreBinding>(-1) { index, item ->
this.textViewLoadMore.text = item.toString()
}
onClick(R.id.textViewLoadMore) { id, index, item ->
logs("(${R.id.textViewLoadMore}, $id) -> $item", LogType.ERROR)
}
}
withItems(mutableListOf("one", 1, "two", 2))
}- Update adapter.
- remove item -
adapter - "eight",adapter - 0 - insert item
- single item
adapter + "asdh",adapter.add(0, "asd") - multiple item
adapter + mutableListOf(),adapter.add(0, mutableListOf<String>())
- single item
- change item -
adapter[2] = "two - 2" - clear adapter list -
adapter.clear()and more on etc.