GestureDetectorCompat not reacting to events

限于喜欢 提交于 2020-08-09 19:52:09

问题


In the following code, I do not get any response to touch events when I setup my GestureDetectorCompat. Could it be because I use data binding? If so, do you have any ideas why and how to get around the problem?

private lateinit var mDetector: GestureDetectorCompat

private fun setupDataBinding() {
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
    binding.lifecycleOwner = this
}

private fun setupViewListener() {
    mDetector = GestureDetectorCompat(this, MyGestureListener())
}


private class MyGestureListener : GestureDetector.SimpleOnGestureListener() {
    private val DEBUG_TAG = "Gestures"

    override fun onDown(event: MotionEvent): Boolean {
        Log.d(DEBUG_TAG, "onDown: $event")
        return true
    }

回答1:


You should override onTouchEvent() and dispatchTouchEvent() in your activity as below:

@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
    this.mDetector.onTouchEvent(motionEvent);
    return super.onTouchEvent(motionEvent);
}

@Override
public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
    super.dispatchTouchEvent(ev);
    return mDetector.onTouchEvent(ev);
}


来源:https://stackoverflow.com/questions/62277239/gesturedetectorcompat-not-reacting-to-events

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!