Kiel
Kiel is a RecyclerView.Adapter with a minimalist and convenient Kotlin DSL which provides utility on top of Android's normal RecyclerView.Adapter.
Most of time:
- We found ourselves repeating same boiler plate codes for
RecyclerView.Adapter
. - We have difficulty in handling
RecyclerView.Adapter
when there are manyviewTypes
.
But now, Kiel may help us to get rid of these problems.
Usage:
Basic Usage:
val recyclerViewAdapter = adapterOf<Text> {
register {
type { Text::class.java }
layoutResource { R.layout.adapter_message_text_item }
viewHolder { ::TextMessageViewHolder }
onViewHolderCreated<TextMessageViewHolder>{ vh->
//you may handle your on click listener
vh.itemView.setOnClickListener {
}
}
onViewHolderBound<Text, TextMessageViewHolder> { vh, _, it ->
vh.messageText.text = it.text
vh.sentAt.text = it.sentAt
}
}
}
recyclerView.adapter = recyclerViewAdapter
Different View Types:
You may register different ViewHolder
s.
val recyclerViewAdapter = adapterOf<MessageViewState> {
register {
type { Text::class.java }
layoutResource { R.layout.adapter_message_text_item }
viewHolder { ::TextMessageViewHolder }
onViewHolderBound<Text, TextMessageViewHolder> { vh, _, it ->
vh.messageText.text = it.text
vh.sentAt.text = it.sentAt
}
}
register {
type { Image::class.java }
layoutResource { R.layout.adapter_message_image_item }
viewHolder { ::ImageMessageViewHolder }
onViewHolderBound<Image, ImageMessageViewHolder> { vh, _, item ->
vh.messageText.text = item.text
vh.sentAt.text = item.sentAt
Glide.with(vh.messageImage)
.load(item.imageUrl)
.into(vh.messageImage)
}
}
}
recyclerView.adapter = recyclerViewAdapter
Handling Events:
As ViewHolder
instance is accessible in:
onViewHolderCreated
onViewHolderBound
onViewHolderBoundWithPayload
You can handle the events in the same way how you did it before.
val recyclerViewAdapter = adapterOf<Text> {
register {
type { Text::class.java }
layoutResource { R.layout.adapter_message_text_item }
viewHolder { ::TextMessageViewHolder }
onViewHolderCreated<TextMessageViewHolder>{ vh->
vh.itemView.setOnClickListener {
}
vh.messageText.addTextChangedListener{text ->
}
}
onViewHolderBound<Text, TextMessageViewHolder> { vh, _, it ->
vh.messageText.text = it.text
vh.sentAt.text = it.sentAt
}
}
}
recyclerView.adapter = recyclerViewAdapter
DiffUtil:
You may provide your custom DiffUtil.ItemCallback
by extending RecyclerDiffUtilCallback
with diffUtilCallback
:
val recyclerViewAdapter = adapterOf<MessageViewState> {
diffUtilCallback{ oldItems, newItems-> RecyclerDiffUtilCallback(oldItems,newItems)}
register {
type { Text::class.java }
layoutResource { R.layout.adapter_message_text_item }
viewHolder { ::TextMessageViewHolder }
onViewHolderBound<Text, TextMessageViewHolder> { vh, _, it ->
vh.messageText.text = it.text
vh.sentAt.text = it.sentAt
}
}
Download
implementation 'me.ibrahimyilmaz:kiel:latestVersion'