简体   繁体   English

不使用 rxjava 如何去抖动

[英]how does debounce without using rxjava

I want to do a thing similar to Debounce in Rxjava, but I don't use rxjava as well as using the other librarys.我想做类似于Rxjava中的 Debounce 的事情,但我不使用 rxjava 以及使用其他库。 Everyone can suggest for me some ways of doing this.每个人都可以为我建议一些这样做的方法。

You can make a function with handler.postDelay() and runnable , something like this:您可以使用 handler.postDelay handler.postDelay()runnable制作 function ,如下所示:

private fun debounce(yourParams: Any, delayMillis: Long) {
        handler.removeCallbacks(runnable)
        runnable = Runnable {
            // Your code here.
        }
        handler.postDelayed(runnable, delayMillis)
    }

I've seen an implementation where you create a static variable and set it to true when clicked and then to false when done, because it is static, it disables all the views that use this type of onClick listener.我见过一个实现,您创建一个 static 变量并在单击时将其设置为 true,然后在完成时将其设置为 false,因为它是 static,它会禁用所有使用此类onClick侦听器的视图。

However, part of the debouncing in RxJava is time related, so i guess you could also add a timer.然而,RxJava 中的部分去抖动是与时间相关的,所以我想你也可以添加一个计时器。

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

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