Skip to content

Instantly share code, notes, and snippets.

@LDuncAndroid
Created January 21, 2020 14:00
Show Gist options
  • Save LDuncAndroid/1bc0f02ada402b244e1ff062900fb483 to your computer and use it in GitHub Desktop.
Save LDuncAndroid/1bc0f02ada402b244e1ff062900fb483 to your computer and use it in GitHub Desktop.
Data Bound Adapter
abstract class DataBoundAdapter<T, V : ViewDataBinding>(
diffCallback: DiffUtil.ItemCallback<T>
) : ListAdapter<T, DataBoundViewHolder<V>>(diffCallback) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataBoundViewHolder<V> {
val binding = createBinding(parent)
return DataBoundViewHolder(binding)
}
protected abstract fun createBinding(parent: ViewGroup): V
override fun onBindViewHolder(holder: DataBoundViewHolder<V>, position: Int) {
bind(holder.binding, getItem(position))
holder.binding.executePendingBindings()
}
protected abstract fun bind(binding: V, item: T)
}
class DataBoundViewHolder<out T : ViewDataBinding> constructor(val binding: T) :
RecyclerView.ViewHolder(binding.root)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment