Home » Android » java – Able to click on two items at the same time in a RecyclerView

java – Able to click on two items at the same time in a RecyclerView

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a list of items in a RecyclerView and i set the onClickListener in the onBindViewHolder for each view. The click listener works just fine, the issue is that I can click on two items in the list at the same time and both of them will run their onClick method. When you have ListViews if you try to click on two items at the same time it does not allow you.

For example:
Lets say you are already touching on an item in a listview and during that time you try to touch another item it won’t let you. Recyclerview allows that.

How can we make the RecyclerView to work like a ListView for when clicking?

Below is my implementation

public class DataCardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private ArrayList<Data> mDatas = new ArrayList<>();
    private Data mData;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View card = LayoutInflater.from(mContext).inflate(R.layout.card, parent, false);
        return  new DataCardViewHolder(mContext, card, mData);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Data data = mDatas.get(position);
        ((DataCardViewHolder )holder).configureDataCard(data);
    }

    public static class DataCardViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private Context mContext;
        private Data mData;

        public DataCardViewHolder(Context context, View view, Data data) {
            super(view);
            mContext = context;
            mData= data;
        }

        public void configureDataCard(final Data data) {
            mData= data;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Log.v("DataCardViewHolder","onClick with data: " + mData.getData().toString());
        }
    }
}
How to&Answers:

My RecyclerView is programmatically add in Java but not in xml. And I try this and it works:

mRecyclerView.setMotionEventSplittingEnabled(false);

If your RecyclerView is add in xml, you may try adding this in your RecyclerView:

android:splitMotionEvents="false"

And now in the recycler-list when you click on one item and don’t let go, you can not click another item.

Answer:

Unfortunately, RecyclerView will not handle that for you. Create a Handler with timeout:

public class DelayedClick {
    protected boolean canClick = true;
    protected Handler myHandler = new Handler();

    private Runnable mMyRunnable = new Runnable()
    {
        @Override
        public void run() {
            canClick = true;
        }
    };

    public boolean getState() {
        if(canClick) {
            myHandler.postDelayed(mMyRunnable, 200);
            canClick = false;
            return true;
        }
        else return false;
    }
}

@Override
public void onClick(View v) {
    if (!reClick.getState()) {
        return;
    }
    //Code to execute on click
}