为什么多次调用Recyclerview适配器onBindViewHolder?

时间:2019-07-19 10:32:55

标签: android android-recyclerview google-cloud-firestore android-diffutils

我在回收站视图中使用this tutorial来实现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的新手。那么,是否有可能发生?还是代码有什么问题?

1 个答案:

答案 0 :(得分:1)

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

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

您编辑的代码:

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());
            }
        });
}
相关问题