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