问题
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