0

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?

Hayk Mkrtchyan
  • 2,835
  • 3
  • 19
  • 61
  • Your problem is solved? – MMG Apr 26 '20 at 04:33
  • Hi. Actually I'll try it tomorrow. Btw I have a question. I need to determine the first click and second click elapsed time. So I have a recyclerview and my check condition that I want, if the item that user clicked is the same, I need to count determine elapsed time between first and second click. If the items are different, I need to execute them both, one by one. If I will click multiple times on the same item and after that click on another item, I need to execute first item last click and second item click. I think you understood)) Thank you. – Hayk Mkrtchyan Apr 26 '20 at 12:55
  • So when I use handler and user clicks multiple times on same item, I don't have to execute them all and I need to execute only the last click in handler. So one solution is to remove callbacks and messages from handler on every click. This will only execute last click if user clicks multiple times on button. But this will not work for different items. So if there was a way to give an id to the handler and if I could remove specific handler callback, I could solve my problem also for different items. – Hayk Mkrtchyan Apr 26 '20 at 12:59
  • I have also a criteria, that on click I need to change button state and after 1 sec send request to the server. And if the request to the server will fail, I need to revert the button state. So I store all button clicks in list with it's position and if any of the requests fails, I revert the corresponding button state. – Hayk Mkrtchyan Apr 26 '20 at 13:03
  • Though I will try your answer, if it will help, I'll mark it as accepted. – Hayk Mkrtchyan Apr 26 '20 at 13:04
  • Hi, please don't add new questions to main one, it's better to ask new questions. Comment is not good place to ask question. – MMG Apr 26 '20 at 13:10

1 Answers1

1
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

MMG
  • 3,226
  • 5
  • 16
  • 43
  • Man this solution worked also for different items, I don't know how it worked yet, but I'll accept this answer. Thank you :) – Hayk Mkrtchyan Apr 27 '20 at 06:35