Skip to content

Instantly share code, notes, and snippets.

@muthuraj57
Created February 18, 2018 09:01
Show Gist options
  • Save muthuraj57/70545c9988a01b05381bac33bcd40741 to your computer and use it in GitHub Desktop.
Save muthuraj57/70545c9988a01b05381bac33bcd40741 to your computer and use it in GitHub Desktop.
abstract class RecyclerBaseAdapter : RecyclerView.Adapter<RecyclerViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
RecyclerViewHolder(DataBindingUtil.inflate<ViewDataBinding>(LayoutInflater.from(parent.context), viewType, parent, false))
override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
getViewModel(position)
?.let {
val bindingSuccess = holder.binding.setVariable(BR.viewModel, it)
if (!bindingSuccess) {
throw IllegalStateException("Binding ${holder.binding} viewModel variable name should be 'viewModel'")
}
}
}
override fun getItemViewType(position: Int) = getLayoutIdForPosition(position)
abstract fun getLayoutIdForPosition(position: Int): Int
abstract fun getViewModel(position: Int): Any?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment