Android RxJava Debounce onNext()

Android RxJava Debounce onNext(),android,kotlin,rx-java,rx-java2,Android,Kotlin,Rx Java,Rx Java2,我正在尝试为我的SwitchCompat取消点击/滑动,但没有成功。 下面的代码看起来不错,不过onNext没有经过debounce()即:当用户垃圾邮件发送开关时,每次单击都会调用onNext,而不是因为debounce而忽略 它应该可以工作了,这是RxJava的一个bug吗 timer = Observable.create { subscriber: Subscriber<in Void>? -> super.setOnCheckedChangeL

我正在尝试为我的SwitchCompat取消点击/滑动,但没有成功。 下面的代码看起来不错,不过onNext没有经过debounce()即:当用户垃圾邮件发送开关时,每次单击都会调用onNext,而不是因为debounce而忽略

它应该可以工作了,这是RxJava的一个bug吗

    timer = Observable.create { subscriber: Subscriber<in Void>? ->
        super.setOnCheckedChangeListener { buttonView, isChecked ->
            subscriber?.onNext(null)
        }
    }
    timer.debounce(3,TimeUnit.SECONDS)
timer=Observable.create{subscriber:subscriber?->
super.setOnCheckedChangeListener{按钮视图,已检查->
订户?.onNext(空)
}
}
计时器。去盎司(3,时间单位。秒)

如果这是您的实际代码,我认为问题根本与
去盎司无关。与大多数Rx操作符一样,
debounce
返回一个新的可观测值-它不会更改
timer
指向的可观测值

因此,由于您没有将
debounce
返回的引用存储在任何位置,因此基本上不会发生任何事情


尝试:
timer=timer.debounce(3,TimeUnit.SECONDS)
如果这是您的实际代码,我认为问题根本与
debounce
无关。与大多数Rx操作符一样,
debounce
返回一个新的可观测值-它不会更改
timer
指向的可观测值

因此,由于您没有将
debounce
返回的引用存储在任何位置,因此基本上不会发生任何事情

尝试:
timer=timer.debounce(3,TimeUnit.SECONDS)
您应该使用Jake Wharton提供的。它将android小部件包装到Rx中。为您提供了RxCompoundButton,您可以这样使用:

RxCompoundButton.checkedChanges(switchCompat)
    .debounce(3, TimeUnit.SECONDS)
    .subscribe();
你应该用杰克·沃顿的。它将android小部件包装到Rx中。为您提供了RxCompoundButton,您可以这样使用:

RxCompoundButton.checkedChanges(switchCompat)
    .debounce(3, TimeUnit.SECONDS)
    .subscribe();
如果您使用的是Kotlin&。您可以在自定义的CompoundButton中进行表示

import com.jakewharton.rxbinding.widget.checkedChanges

class CustomCompoundButton: CompoundButton {
    constructor(context: Context): super(context, null)

    constructor(context: Context, attrs: AttributeSet?): super(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int): super(context, attrs, defStyleAttr, 0)

    fun throttledCheckedChanges() { 
        return this.checkedChanges().debounce(3, TimeUnit.SECONDS)
    }
    // OR
    val throttledCheckedChanges by lazy {
        this.checkedChanges().debounce(3, TimeUnit.SECONDS)
    }

}

 // Use it in your activity/ fragment
 customCompoundButton.throttledCheckedChanges().subscribe { /* do sth */ }
 // OR
 customCompoundButton.throttledCheckedChanges.subscribe { /* do sth */ }
如果不允许在项目中使用。如果您使用的是Kotlin&,您应该看看如何创建Rx包装器。

。您可以在自定义的CompoundButton中进行表示

import com.jakewharton.rxbinding.widget.checkedChanges

class CustomCompoundButton: CompoundButton {
    constructor(context: Context): super(context, null)

    constructor(context: Context, attrs: AttributeSet?): super(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int): super(context, attrs, defStyleAttr, 0)

    fun throttledCheckedChanges() { 
        return this.checkedChanges().debounce(3, TimeUnit.SECONDS)
    }
    // OR
    val throttledCheckedChanges by lazy {
        this.checkedChanges().debounce(3, TimeUnit.SECONDS)
    }

}

 // Use it in your activity/ fragment
 customCompoundButton.throttledCheckedChanges().subscribe { /* do sth */ }
 // OR
 customCompoundButton.throttledCheckedChanges.subscribe { /* do sth */ }

如果不允许在项目中使用。您应该看看如何创建Rx包装器。

只需澄清一件事:您知道debounce的工作时间跨度吗?要使去盎司工作,您必须根据您的代码在3秒内检查和取消检查开关。试着用一个按钮来做模拟,然后检查它是否有效。谢谢你的回复,是的,我会的,这正是我想要实现的。你能发布你所有与之相关的代码吗?因为你给出的代码没有解释你是如何实现它的。看起来您正在创建一个自定义Swtich类,然后做了一些事情。只需澄清一件事:您知道debounce在timespan中工作吗?要使去盎司工作,您必须根据您的代码在3秒内检查和取消检查开关。试着用一个按钮来做模拟,然后检查它是否有效。谢谢你的回复,是的,我会的,这正是我想要实现的。你能发布你所有与之相关的代码吗?因为你给出的代码没有解释你是如何实现它的。看起来你正在创建一个自定义的Swtich类,然后做一些事情。谢谢,伙计,完成了任务!谢谢你,伙计,你做到了!