Skip to content

Instantly share code, notes, and snippets.

@nisaefendioglu
Created April 28, 2023 12:06
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 nisaefendioglu/74241d8360ae4ecd00614dc5500f48c3 to your computer and use it in GitHub Desktop.
Save nisaefendioglu/74241d8360ae4ecd00614dc5500f48c3 to your computer and use it in GitHub Desktop.
class MyPagingDataAdapter : PagingDataAdapter<Data, MyPagingDataAdapter.ViewHolder>(diffCallback) {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val data = getItem(position)
data?.let {
holder.bind(data)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = DataItemBinding.inflate(layoutInflater, parent, false)
return ViewHolder(binding)
}
class ViewHolder(private val binding: DataItemBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(data: Data) {
binding.data = data
binding.executePendingBindings()
}
}
companion object {
private val diffCallback = object : DiffUtil.ItemCallback<Data>() {
override fun areItemsTheSame(oldItem: Data, newItem: Data): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Data, newItem: Data): Boolean {
return oldItem == newItem
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment