eegooDeveloper
eegooDeveloper

Reputation: 405

Why Recyclerview adapter onBindViewHolder is called more than once?

I use this tutorial to implement DiffUtil in my recyclerview. My aim is to add single item to the bottom of the recyclerview without reloading the rest of the recyclerview. I used firestore addSnapshotListener to call the adapter. The problem is the onBindViewHolder is called multiple times (ie, no of items present in the list). I dont think that is suppose to happen when using DiffUtil, right? It should only call onBindViewHolder for the item that is added to recyclerview, right?

This is the code whre I call the adapter:

@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());
            }
        });
}

This is the adapter class:

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);
            }
        }
    }

I am new to DiffUtil. So, is it suppose to happen ? Or Is there anything wrong with the code?

Upvotes: 0

Views: 632

Answers (1)

Marat Zangiev
Marat Zangiev

Reputation: 1402

Every time when you get callback from Firestore you recreate your CommentsListAdapter

Pull the adapter into the global variable in your Activity and call only adapter.submitList(comments); in Firestore callback

Your Edited code:

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());
            }
        });
}

Upvotes: 1

Related Questions