Ability to simultaneously click on two elements in RecyclerView

I have a list of items in a RecyclerView, and I set onClickListener to onBindViewHolder for each view. The click listener works very well, the problem is that I can simultaneously click on two items in the list, and both of them will run their onClick method. If you have ListViews, if you are trying to click on two items at the same time, this does not allow you.

For instance:
Suppose you are already touching an item in a list, and during this time you are trying to touch another item that it will not allow you to. Recyclerview allows this.

How can we make RecyclerView work like a ListView when clicked?

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()); } } } 
+6
source share
2 answers

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

  mRecyclerView.setMotionEventSplittingEnabled(false); 

If your RecyclerView is added to xml, you can try adding this to your RecyclerView:

  android:splitMotionEvents="false"; 

And now in the list, when you click on one item and do not release, you cannot click on another item.

+6
source

Unfortunately, RecyclerView will not handle this for you. Create a Handler with a 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 } 
0
source

All Articles