Skip to content

Instantly share code, notes, and snippets.

@maskaravivek
Created June 10, 2020 16:13
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 maskaravivek/fcd4b06bf2491c58ebe7a388acd22d0a to your computer and use it in GitHub Desktop.
Save maskaravivek/fcd4b06bf2491c58ebe7a388acd22d0a to your computer and use it in GitHub Desktop.
class UserListAdapter internal constructor() :
PagedListAdapter<User, UserViewHolder>(DIFF_CALLBACK) {
/**
* Initializes the view holder with contribution data
*/
override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
holder.bind(getItem(position))
}
/**
* Creates the new View Holder which will be used to display items(contributions) using the
* onBindViewHolder(viewHolder,position)
*/
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): UserViewHolder {
val inflater = LayoutInflater.from(parent.context)
return UserViewHolder(inflater, parent)
}
companion object {
/**
* Uses DiffUtil to calculate the changes in the list
* It has methods that check ID and the content of the items to determine if its a new item
*/
private val DIFF_CALLBACK: DiffUtil.ItemCallback<User> =
object : DiffUtil.ItemCallback<User>() {
override fun areItemsTheSame(
oldContribution: User,
newContribution: User
): Boolean {
return oldContribution == newContribution
}
override fun areContentsTheSame(
oldContribution: User,
newContribution: User
): Boolean {
return oldContribution == newContribution
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment