延迟recyclerview每一项点击功能 [英] Delay recyclerview every item click functionality

查看:95
本文介绍了延迟recyclerview每一项点击功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个recyclerview.我想实现一种功能,但无法实现. 我的RecyclerView项目有2个textView和一个button.

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;
}
}

所以让我解释一下.我的recyclerView项中的按钮使用stateListDrawable,因此单击时变为活动/非活动状态.当用户单击按钮时,按钮变为活动状态,我必须将项目数据(标题,ID)发送到后端.当用户单击非活动按钮时,我向该项目的后端发送删除请求.但是问题是用户可以多次单击同一按钮,而我不想每次都发送make请求.例如.我可以在一秒钟内按同一按钮4次,该按钮变为活动/不活动状态,因此我不想在一秒钟内发出4-5个请求.我认为,解决方案是在点击事件上增加延迟,例如单击后500毫秒或1000毫秒后,将数据发送到后端.我尝试使用Handler,实际上它起作用了.例如.当我点击第二项,第四项和第五项时,每点击一秒钟,我就可以将数据发送到后端.但是也有一个问题.当我多次单击同一按钮时,一秒钟后它将发送到后端多个请求.在启动处理程序之前,我尝试对每次单击执行removeCallbacksAndMessages(),但是也存在另一个问题.例如.当我在第二项上多次单击后,在单击第三项时,我仅将第三项数据发送到后端.但是我想将第二项和第三项数据发送到后端.那么有人可以帮助我实现此功能吗?

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?

推荐答案

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

这篇关于延迟recyclerview每一项点击功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆