提交 b60849d6 编写于 作者: I Ian Lake

Make CommentAdapter extend ListAdapter

Use `ListAdapter` to calculate the DiffUtil off of the main thread.

Test: ./gradlew cC
上级 28f265ab
...@@ -17,70 +17,45 @@ ...@@ -17,70 +17,45 @@
package com.example.android.persistence.ui; package com.example.android.persistence.ui;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.AsyncDifferConfig;
import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.ViewGroup; import android.view.ViewGroup;
import com.example.android.persistence.databinding.CommentItemBinding; import com.example.android.persistence.databinding.CommentItemBinding;
import com.example.android.persistence.model.Comment; import com.example.android.persistence.db.entity.CommentEntity;
import com.example.android.persistence.R; import com.example.android.persistence.R;
import java.util.List; public class CommentAdapter extends ListAdapter<CommentEntity, CommentAdapter.CommentViewHolder> {
public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder> {
private List<? extends Comment> mCommentList;
@Nullable @Nullable
private final CommentClickCallback mCommentClickCallback; private final CommentClickCallback mCommentClickCallback;
public CommentAdapter(@Nullable CommentClickCallback commentClickCallback) { CommentAdapter(@Nullable CommentClickCallback commentClickCallback) {
super(new AsyncDifferConfig.Builder<>(new DiffUtil.ItemCallback<CommentEntity>() {
@Override
public boolean areItemsTheSame(@NonNull CommentEntity old,
@NonNull CommentEntity comment) {
return old.getId() == comment.getId();
}
@Override
public boolean areContentsTheSame(@NonNull CommentEntity old,
@NonNull CommentEntity comment) {
return old.getId() == comment.getId()
&& old.getPostedAt().equals(comment.getPostedAt())
&& old.getProductId() == comment.getProductId()
&& TextUtils.equals(old.getText(), comment.getText());
}
}).build());
mCommentClickCallback = commentClickCallback; mCommentClickCallback = commentClickCallback;
} }
public void setCommentList(final List<? extends Comment> comments) {
if (mCommentList == null) {
mCommentList = comments;
notifyItemRangeInserted(0, comments.size());
} else {
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()
&& TextUtils.equals(old.getText(), comment.getText());
}
});
mCommentList = comments;
diffResult.dispatchUpdatesTo(this);
}
}
@Override @Override
@NonNull @NonNull
public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
...@@ -93,15 +68,10 @@ public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentV ...@@ -93,15 +68,10 @@ public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentV
@Override @Override
public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) { public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) {
holder.binding.setComment(mCommentList.get(position)); holder.binding.setComment(getItem(position));
holder.binding.executePendingBindings(); holder.binding.executePendingBindings();
} }
@Override
public int getItemCount() {
return mCommentList == null ? 0 : mCommentList.size();
}
static class CommentViewHolder extends RecyclerView.ViewHolder { static class CommentViewHolder extends RecyclerView.ViewHolder {
final CommentItemBinding binding; final CommentItemBinding binding;
......
...@@ -75,7 +75,7 @@ public class ProductFragment extends Fragment { ...@@ -75,7 +75,7 @@ public class ProductFragment extends Fragment {
model.getComments().observe(getViewLifecycleOwner(), commentEntities -> { model.getComments().observe(getViewLifecycleOwner(), commentEntities -> {
if (commentEntities != null) { if (commentEntities != null) {
mBinding.setIsLoading(false); mBinding.setIsLoading(false);
mCommentAdapter.setCommentList(commentEntities); mCommentAdapter.submitList(commentEntities);
} else { } else {
mBinding.setIsLoading(true); mBinding.setIsLoading(true);
} }
......
...@@ -21,7 +21,6 @@ import androidx.lifecycle.AndroidViewModel; ...@@ -21,7 +21,6 @@ import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.databinding.ObservableField;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import com.example.android.persistence.BasicApp; import com.example.android.persistence.BasicApp;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册