Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class BindableRecyclerViewAdapter : RecyclerView.Adapter<BindableViewHolder>() {
var itemViewModels: List<ItemViewModel> = emptyList()
private val viewTypeToLayoutId: MutableMap<Int, Int> = mutableMapOf()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindableViewHolder {
val binding: ViewDataBinding = DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
viewTypeToLayoutId[viewType] ?: 0,
parent,
false)
return BindableViewHolder(binding)
}
override fun getItemViewType(position: Int): Int {
val item = itemViewModels[position]
if (!viewTypeToLayoutId.containsKey(item.viewType)) {
viewTypeToLayoutId[item.viewType] = item.layoutId
}
return item.viewType
}
override fun getItemCount(): Int = itemViewModels.size
override fun onBindViewHolder(holder: BindableViewHolder, position: Int) {
holder.bind(itemViewModels[position])
}
fun updateItems(items: List<ItemViewModel>?) {
itemViewModels = items ?: emptyList()
notifyDataSetChanged()
}
}
class BindableViewHolder(private val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(itemViewModel: ItemViewModel) {
binding.setVariable(BR.itemViewModel, itemViewModel)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment