Created
February 18, 2018 09:01
-
-
Save muthuraj57/70545c9988a01b05381bac33bcd40741 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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