Android - Intercept and pass on all touch events

后端 未结 5 1978
名媛妹妹
名媛妹妹 2021-02-02 15:47

I have an overlay ViewGroup that is the size of the screen which I want to use to show an effect when the user interacts with the app, but still passes the onTouch event to any

5条回答
  •  南方客
    南方客 (楼主)
    2021-02-02 16:14

    I tried all of the solutions in the above answers however still had issues receiving all touch events while still allowing children views to consume the events. In my scenario I would like to listen in on events that a child views would consume. I could not receive follow up events after a overlapping child view started consuming events.

    I finally found a solution that works. Using the RxBinding library you can observe all touch events including events that are consumed by overlapping children views.

    Kotlin Code snippet

    RxView.touches(view)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
    
            object  : Observer {
                override fun onCompleted() {
                }
    
                override fun onNext(t: MotionEvent?) {
                    Log.d("motion event onNext $t")
                }
    
                override fun onError(e: Throwable?) {
                }
            }
        )
    

    See here and here for more details.

提交回复
热议问题