Skip to content

Instantly share code, notes, and snippets.

@fraggjkee
Last active May 17, 2020 15:54
Show Gist options
  • Save fraggjkee/36fed3d6c6de9283b277cfb0040aa6f1 to your computer and use it in GitHub Desktop.
Save fraggjkee/36fed3d6c6de9283b277cfb0040aa6f1 to your computer and use it in GitHub Desktop.
class RecyclerViewAdapter : RecyclerView.Adapter<BindingViewHolder>() {
private val items = mutableListOf<RecyclerItem>()
override fun getItemCount(): Int {
return items.size
}
override fun getItemViewType(position: Int): Int {
return getItem(position).layoutId
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): BindingViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding: ViewDataBinding = DataBindingUtil.inflate(inflater, viewType, parent, false)
return BindingViewHolder(binding)
}
override fun onBindViewHolder(
holder: BindingViewHolder,
position: Int
) {
getItem(position).bind(holder.binding)
holder.binding.executePendingBindings()
}
fun updateData(newItems: List<RecyclerItem>) {
this.items.clear()
this.items.addAll(newItems)
notifyDataSetChanged()
}
private fun getItem(position: Int): RecyclerItem {
return items[position]
}
}
class BindingViewHolder(
val binding: ViewDataBinding
) : RecyclerView.ViewHolder(binding.root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment