Created
June 10, 2020 16:13
-
-
Save maskaravivek/fcd4b06bf2491c58ebe7a388acd22d0a 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 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