Skip to content

Instantly share code, notes, and snippets.

@ziginsider
Created July 4, 2021 15:10
Show Gist options
  • Save ziginsider/4d320ea659d18213909743e3fab9ef93 to your computer and use it in GitHub Desktop.
Save ziginsider/4d320ea659d18213909743e3fab9ef93 to your computer and use it in GitHub Desktop.
class StopwatchAdapter: ListAdapter<Stopwatch, StopwatchViewHolder>(itemComparator) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StopwatchViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = StopwatchItemBinding.inflate(layoutInflater, parent, false)
return StopwatchViewHolder(binding)
}
override fun onBindViewHolder(holder: StopwatchViewHolder, position: Int) {
holder.bind(getItem(position))
}
private companion object {
private val itemComparator = object : DiffUtil.ItemCallback<Stopwatch>() {
override fun areItemsTheSame(oldItem: Stopwatch, newItem: Stopwatch): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Stopwatch, newItem: Stopwatch): Boolean {
return oldItem.currentMs == newItem.currentMs &&
oldItem.isStarted == newItem.isStarted
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment