Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class CommentsRecyclerViewAdapter : ListAdapter<PokemonVM, CommentsRecyclerViewAdapter.ViewHolder>(PokeDiff()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
ListViewItemPokemonBinding.inflate(
LayoutInflater.from(parent.context), parent, false))
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = getItem(position)
holder.apply {
bindView(item)
itemView.tag = item
}
}
class ViewHolder(
private val binding: ListViewItemPokemonBinding
) : RecyclerView.ViewHolder(binding.root) {
fun bindView(pokeVm: PokemonVM) {
binding.apply {
viewmodel = pokeVm
executePendingBindings()
}
}
}
}
private class PokeDiff : DiffUtil.ItemCallback<PokemonVM>() {
override fun areItemsTheSame(old: PokemonVM, new: PokemonVM): Boolean {
return old.name != new.name
}
override fun areContentsTheSame(old: PokemonVM, new: PokemonVM): Boolean {
return old == new
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.