0

我使用本教程在我的 recyclerview中实现DiffUtil 。我的目标是将单个项目添加到 recyclerview 的底部,而无需重新加载 recyclerview 的其余部分。我使用 firestore addSnapshotListener 来调用适配器。问题是onBindViewHolder被多次调用(即列表中没有项目)。我认为使用 DiffUtil 时不会发生这种情况,对吧?它应该只为添加到 recyclerview 的项目调用 onBindViewHolder,对吗?

这是我调用适配器的代码:

@Override
protected void onStart()
{
    super.onStart();

    reference
        .addSnapshotListener((Activity)context, new EventListener<QuerySnapshot>()
        {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots,
                                @Nullable FirebaseFirestoreException e)
            {
                if (e != null)
                {
                    Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show();
                    return;
                }

                CommentsListAdapter adapter = new CommentsListAdapter(context);
                commentsRecyclerView.setAdapter(adapter);

                comments = new ArrayList<>();
                for (QueryDocumentSnapshot snapshot : queryDocumentSnapshots)
                {
                    Comment comment = snapshot.toObject(Comment.class).withId(snapshot.getId());
                    comments.add(comment);
                }
                adapter.submitList(comments);
                commentsRecyclerView.smoothScrollToPosition(adapter.getItemCount());
            }
        });
}

这是适配器类:

class CommentsListAdapter extends ListAdapter<Comment, CommentsListAdapter.CommentsViewHolder>
    {
        private Context context;

        protected CommentsListAdapter(Context context)
        {
            super(DIFF_CALLBACK);
            this.context = context;
        }

        private static final DiffUtil.ItemCallback<Comment> DIFF_CALLBACK = new DiffUtil.ItemCallback<Comment>()
        {
            @Override
            public boolean areItemsTheSame(@NonNull Comment oldItem, @NonNull Comment newItem)
            {
                return oldItem.commentId.equals(newItem.commentId);
            }

            @Override
            public boolean areContentsTheSame(@NonNull Comment oldItem, @NonNull Comment newItem)
            {
                return oldItem.commentId.equals(newItem.commentId);
            }
        };

        @NonNull
        @Override
        public CommentsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
        {
            View itemView = LayoutInflater.from(context)
                    .inflate(R.layout.comment_list_item, parent, false);
            return new CommentsViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(@NonNull final CommentsViewHolder holder, int position)
        {
            System.out.println("POSITION: " + position);
            holder.commentText.setText(getItem(position).getComment());
            holder.timeText.setText(getItem(position).getCommentDateCreated());
        }

        public class CommentsViewHolder extends RecyclerView.ViewHolder
        {
            private TextView commentText;
            private TextView timeText;

            public CommentsViewHolder(@NonNull View itemView)
            {
                super(itemView);
                commentText = itemView.findViewById(R.id.commentText);
                timeText = itemView.findViewById(R.id.timeText);
            }
        }
    }

我是 DiffUtil 的新手。那么,它应该发生吗?或者代码有什么问题吗?

4

1 回答 1

1

每次从 Firestore 收到回调时,您都会重新创建您的CommentsListAdapter

将适配器拉入 Activity 中的全局变量并仅adapter.submitList(comments);在 Firestore 回调中调用

您编辑的代码:

CommentsListAdapter adapter = new CommentsListAdapter(context);
@Override
protected void onStart()
{
    super.onStart();
    commentsRecyclerView.setAdapter(adapter);
    reference
        .addSnapshotListener((Activity)context, new EventListener<QuerySnapshot>()
        {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots,
                                @Nullable FirebaseFirestoreException e)
            {
                if (e != null)
                {
                    Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show();
                    return;
                }

                comments = new ArrayList<>();
                for (QueryDocumentSnapshot snapshot : queryDocumentSnapshots)
                {
                    Comment comment = snapshot.toObject(Comment.class).withId(snapshot.getId());
                    comments.add(comment);
                }
                adapter.submitList(comments);
                commentsRecyclerView.smoothScrollToPosition(adapter.getItemCount());
            }
        });
}
于 2019-07-19T13:51:16.257 回答