Created
September 26, 2017 07:42
-
-
Save mikkipastel/fc4e85aeadd4d6837c525f1c8d860d66 to your computer and use it in GitHub Desktop.
paging library with Room in Android Architecture Component
This file contains hidden or 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 UserAdapter extends PagedListAdapter<User, UserViewHolder> { | |
public UserAdapter() { | |
super(DIFF_CALLBACK); | |
} | |
@Override | |
public void onBindViewHolder(UserViewHolder holder, int position) { | |
User user = getItem(position); | |
if (user != null) { | |
holder.bindTo(user); | |
} else { | |
// Null defines a placeholder item - PagedListAdapter will automatically invalidate | |
// this row when the actual object is loaded from the database | |
holder.clear(); | |
} | |
} | |
public static final DiffCallback<User> DIFF_CALLBACK = new DiffCallback<User>() { | |
@Override | |
public boolean areItemsTheSame(@NonNull User oldUser, @NonNull User newUser) { | |
// User properties may have changed if reloaded from the DB, but ID is fixed | |
return oldUser.getId() == newUser.getId(); | |
} | |
@Override | |
public boolean areContentsTheSame(@NonNull User oldUser, @NonNull User newUser) { | |
// NOTE: if you use equals, your object must properly override Object#equals() | |
// Incorrectly returning false here will result in too many animations. | |
return oldUser.equals(newUser); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment