RxAndroid - 使用RxView.touches清除其他处理程序

时间:2017-07-09 19:12:28

标签: kotlin rx-android

我正在尝试向FabricView添加触摸处理程序,但似乎这样做我无法再绘制草图了。实际上,FabricView本身定义了一个Touch处理程序,但似乎我的尝试 - 使用RxAndroid2 - 清除了预定义的处理程序,而不是链接到它。

我希望能够在不清除现有处理程序的情况下使用我的处理程序。

这是我主要活动的一部分(sketchview是FabricView的一个实例)

sketchView.backgroundMode = FabricView.BACKGROUND_STYLE_NOTEBOOK_PAPER
    RxView.touches(sketchView, {_ -> true}).subscribe { // setting to false let me sketch, but I don't get any toast
        event -> when(event.action){
            MotionEvent.ACTION_DOWN -> {
                toast("Starting")
            }
            MotionEvent.ACTION_UP -> {
                toast("Stopping")
            }
            else -> {}
        }
    }

这是我app.gradle的摘录:

compile 'com.github.antwankakki:FabricView:latest'
compile 'com.rmtheis:tess-two:7.0.0'
compile "io.reactivex.rxjava2:rxjava:2.1.0"
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
compile "org.jetbrains.anko:anko-commons:$anko_version"

为了使用FabricView,我们必须在根gradle构建的allProjects部分中包含JitPack。

allprojects {
    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
        mavenCentral()
        maven { url "https://jitpack.io" }
    }
}

这是the page of the FabricView source code

1 个答案:

答案 0 :(得分:3)

RxView.touches()会返回ViewTouchObservable,在订阅时会调用view.setOnTouchListener(listener)来观察触摸事件。

  

警告:创建的observable使用View.setOnTouchListener()来观察触摸。一次只能有一个observable用于视图。

就像这样你将触摸事件绑定到这个可观察的事物上。它返回true,因此onTouchEvent()不会被调用。要同时使用这两种行为,您可能需要稍微重写ViewTouchObservable并返回false