Skip to content

Instantly share code, notes, and snippets.

@venik
Created March 20, 2019 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save venik/180f3d88a4ecef0e0aaf67515c6bb518 to your computer and use it in GitHub Desktop.
Save venik/180f3d88a4ecef0e0aaf67515c6bb518 to your computer and use it in GitHub Desktop.
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