Created
December 30, 2019 11:46
-
-
Save MuratVarol/03e86015337a81aed2076b3fba4b6f4d 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
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