Created
March 20, 2019 18:53
-
-
Save venik/180f3d88a4ecef0e0aaf67515c6bb518 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
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