Skip to content

Instantly share code, notes, and snippets.

@MuratVarol
Created December 30, 2019 11:46
Show Gist options
  • Save MuratVarol/03e86015337a81aed2076b3fba4b6f4d to your computer and use it in GitHub Desktop.
Save MuratVarol/03e86015337a81aed2076b3fba4b6f4d to your computer and use it in GitHub Desktop.
open class BaseRecyclerAdapter<T>(
private var modelList: List<T>,
private val itemLayoutId: Int,
private val viewModel: ViewModel?
) : RecyclerView.Adapter<BaseViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
val layoutInflater: LayoutInflater = LayoutInflater.from(parent.context)
val binding: ViewDataBinding =
DataBindingUtil.inflate(layoutInflater, itemLayoutId, parent, false)
return object : BaseViewHolder(binding) {
override fun bindData(position: Int) {
val model = modelList[position]
itemBinding.setVariable(BR.model, model)
viewModel?.let {
itemBinding.setVariable(BR.viewModel, it)
}
}
}
}
override fun getItemCount(): Int = modelList.size
override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
holder.bindData(position)
}
fun updateData(list: List<T>) {
modelList = list
notifyDataSetChanged()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment