Skip to content

Instantly share code, notes, and snippets.

@yuanliwei
Created January 16, 2019 15:38
Show Gist options
  • Save yuanliwei/287f0f5cddead73cc99d7c85c63f498a to your computer and use it in GitHub Desktop.
Save yuanliwei/287f0f5cddead73cc99d7c85c63f498a to your computer and use it in GitHub Desktop.
androidx.recyclerview.widget.DiffUtil
            DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffUtil.Callback() {
                @Override
                public int getOldListSize() {
                    return mCommentList.size();
                }

                @Override
                public int getNewListSize() {
                    return comments.size();
                }

                @Override
                public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
                    Comment old = mCommentList.get(oldItemPosition);
                    Comment comment = comments.get(newItemPosition);
                    return old.getId() == comment.getId();
                }

                @Override
                public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
                    Comment old = mCommentList.get(oldItemPosition);
                    Comment comment = comments.get(newItemPosition);
                    return old.getId() == comment.getId()
                            && old.getPostedAt() == comment.getPostedAt()
                            && old.getProductId() == comment.getProductId()
                            && Objects.equals(old.getText(), comment.getText());
                }
            });
            mCommentList = comments;
            diffResult.dispatchUpdatesTo(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment