Y
Y
Yaroslav Pronin2016-07-31 14:29:13
Java
Yaroslav Pronin, 2016-07-31 14:29:13

Android RecyclerView. How to allow click on element during notifyDataSetChanged()?

Hello. I am using a RecyclerView adapter to display items from a ProgressBar. Accordingly, the code often calls notifyDataSetChanged() and at this time the user cannot click and thereby open the element, since the ViewHolder is redrawn and OnClickListener does not work. Is it possible to solve this problem? Of course, I could use notifyItemChanged() (no problem with it), but the adapter uses a HashMap with String keys, and there will be some problems getting the item in onBindViewHolder() by position in the adapter.
Here is an example code:

public ViewHolder(View itemView, ClickListener listener)
       {
            super(itemView);

            this.listener = listener;
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);

            ...
       }

        ...

       @Override
        public void onClick(View view)
        {
            int position = getAdapterPosition();

            if (listener != null && position >= 0) {
                Item item = items.get(position);
                listener.onItemClicked(position, item);
            }
        }

        @Override
        public boolean onLongClick(View view)
        {
            int position = getAdapterPosition();

            if (listener != null && position >= 0) {
                Item item = items.get(position);
                listener.onItemLongClicked(position, item);

                return true;
            }

            return false;
        }

        public interface ClickListener
        {
            void onItemClicked(int position, Item item);

            boolean onItemLongClicked(int position, Item item);
        }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question