本文介绍了GestureDetectorCompat对事件无反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中,设置我的GestureDetectorCompat时,我对触摸事件没有任何反应.可能是因为我使用数据绑定吗?如果是这样,您对为什么以及如何解决该问题有任何想法吗?

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
    }

推荐答案

您应该在活动中覆盖onTouchEvent()dispatchTouchEvent(),如下所示:

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);
}

这篇关于GestureDetectorCompat对事件无反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 16:38