Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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());
        }
    }
}
like image 322
KikiTheMonk Avatar asked Sep 27 '16 10:09

KikiTheMonk


1 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.

like image 56
chinben Avatar answered Nov 08 '22 07:11

chinben