I tried the following code:
view.setEnabled(false);
myReqRecyclerView.setEnabled(false);
The first line disables the current view which is clicked. After one click, I want to set all items non clickable so that double click should not happen. It seems that the second line is not working.
First Simple option
but quite bad actually would by to use System time :
long mLastClickTime = 0;
youradapter.setOnItemClickListener(new youradapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
....//your code
}
});
but for a clean programming I would recommend doing this :
A bit trickier but as simple
In your Adapter introduce a boolean variable, please pay attention to the viewHolder() and onBindViewHolder() methods :
private boolean clickable = true;
@Override
public void onBindViewHolder(@NonNull myAdapter.ViewHolder viewHolder, int position) {
if (!clickable){
viewHolder.cardView.setClickable(false);
} else {
viewHolder.cardView.setClickable(true);
}
//....
}
public void setClickable(boolean clickable) {
this.clickable = clickable;
myadapter.notifyDataSetChanged();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
public ViewHolder(View itemView, final myViewHolder.OnItemClickListener listener){
super(itemView);
//..
cardView = itemView.findViewById(R.id.cardView);
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onItemClick(position);
}
}
}
});
}
}
and use myadapter.setClickable(state) as you wish
like this :
youradapter.setOnItemClickListener(new youradapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
myadapter.setClickable(false);
....//your code
}
});
and maybe in your onResume() :
myadapter.setClickable(true);
try view.setClickable(false); on the view which you dont want to be clickable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With