简体   繁体   English

延迟回收者查看每个项目的点击功能

[英]Delay recyclerview every item click functionality

I have a recyclerview .我有一个recyclerview I want to achieve one functionality, but I can't implement it.我想实现一个功能,但我无法实现。 My RecyclerView item has 2 textView and a button .我的 RecyclerView 项目有 2 textView和一个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.我的recyclerView项目中的按钮使用stateListDrawable ,因此它在单击时变为活动/不活动。 When user clicks on a button, and it becomes active, I have to send the item data (title, id) to backend.当用户单击一个按钮并使其变为活动状态时,我必须将项目数据(标题、ID)发送到后端。 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.但问题是用户可以多次点击同一个按钮,我不想每次都发送请求。 Eg 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.例如,我可以在一秒钟内按同一个按钮 4 次,然后它会变为活动/非活动状态,所以我不想在一秒钟内发出 4-5 个请求。 What I think, solution is to add a delay on that click events, and eg after 500ms or 1000ms after click send data to backend.我认为,解决方案是在该点击事件上添加延迟,例如在点击后 500 毫秒或 1000 毫秒后将数据发送到后端。 I tried to use Handler and actually it worked.我尝试使用Handler并且实际上它起作用了。 Eg when I clicked on 2nd, then 4th and 5th items, after a second of every click, I could send the data to backend.例如,当我点击第 2 项、第 4 项和第 5 项时,每次点击一秒钟后,我就可以将数据发送到后端。 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.在启动处理程序之前,我尝试在每次单击时执行removeCallbacksAndMessages() ,但还有另一个问题。 Eg 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?那么任何人都可以帮我实现这个功能吗?

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有关详细信息,请参见此处: Android 防止双击按钮

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM