Created
April 28, 2023 12:06
-
-
Save nisaefendioglu/74241d8360ae4ecd00614dc5500f48c3 to your computer and use it in GitHub Desktop.
This file contains 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 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