【问题标题】:Preventing rapid clicks with RXJava使用 RXJava 防止快速点击
【发布时间】:2014-03-05 17:23:23
【问题描述】:

我正在编写一个 android 应用程序并使用 rxjava 来处理用户输入事件。基本上我想要做的是,在单击按钮时发出,然后在之后的一段时间内(例如一两秒)放弃后续发射,基本上是为了防止必须处理多次单击按钮。

【问题讨论】:

标签: rx-java


【解决方案1】:

【讨论】:

【解决方案2】:

为了防止快速点击,我使用此代码

RxView.clicks(your_view)
            .throttleFirst(300, TimeUnit.MILLISECONDS)
            .subscribe {
                //on click
            }

【讨论】:

    【解决方案3】:

    继续zsxwing的回答:

    如果你没有使用 RxBinding 库而只使用 RxJava 那么,

    io.reactivex.Observable.just(view_obj)
                    .throttleFirst(1, TimeUnit.SECONDS)// prevent rapid click for 1 seconds
                    .blockingSubscribe(o -> {                     
                        startActivity(...);
                    });
    

    【讨论】:

    • blockingSubscribe 永远不会被调用。
    【解决方案4】:

    这可以通过共享去抖动和缓冲操作符来完成

    Observable<Object> tapEventEmitter = _rxBus.toObserverable().share();
    Observable<Object> debouncedEventEmitter = tapEventEmitter.debounce(1, TimeUnit.SECONDS);
    Observable<List<Object>> debouncedBufferEmitter = tapEventEmitter.buffer(debouncedEventEmitter);
    
    debouncedBufferEmitter.buffer(debouncedEventEmitter)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Action1<List<Object>>() {
          @Override
          public void call(List<Objecenter code heret> taps) {
            _showTapCount(taps.size());
          }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多