Reputation: 3255
I have a recyclerview
. I want to achieve one functionality, but I can't implement it.
My RecyclerView item has 2 textView
and a button
.
public class Task {
private String id;
private String title;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
So let me explain a little. The button in my recyclerView
item uses stateListDrawable
, so it becomes active/inactive on click. When user clicks on a button, and it becomes active, I have to send the item data (title, id) to backend. And when the user clicks on inactive button, I send a delete request to backend for that item. But the problem is that user can click multiple times on the same button and I don't want to send make request every time. E.g. I can press the same button 4 times in a second and it becomes active/inactive, so I don't want to make 4-5 requests in a second. What I think, solution is to add a delay on that click events, and e.g. after 500ms or 1000ms after click send data to backend. I tried to use Handler
and actually it worked. E.g. when I clicked on 2nd, then 4th and 5th items, after a second of every click, I could send the data to backend. But there is a problem too. When I click on same button multiple times, after a second it sends to backend multiple requests. I've tried to do removeCallbacksAndMessages()
on every click before starting the handler, but there is another problem too. E.g. When I click multiple times on 2nd item and after that I click on 3rd item, I send only the 3rd item data to backend. But I want to send 2nd and 3rd items data to backend. So can anyone help me to achieve this functionality?
Upvotes: 0
Views: 1061
Reputation: 3268
private long mLastClickTime = 0;
...
// inside onCreate or so:
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// mis-clicking prevention, using threshold of 1000 ms
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
return;
}
mLastClickTime = SystemClock.elapsedRealtime();
// do your magic here
}
}
See here for more details: Android Preventing Double Click On A Button
Upvotes: 1