Android爬坑记录——GestureDetector无效
很多小伙伴在第一次使用手势监听的时候,肯定会遇到GestureDetector无效的情况,那么究竟是为什么呢?我们直接来看谷歌官方文档
Whether or not you use GestureDetector.OnGestureListener, it’s best practice to implement an onDown() method that returns true. This is because all gestures begin with an onDown() message. If you return false from onDown(), as GestureDetector.SimpleOnGestureListener does by default, the system assumes that you want to ignore the rest of the gesture, and the other methods of GestureDetector.OnGestureListener never get called. This has the potential to cause unexpected problems in your app. The only time you should return false from onDown() is if you truly want to ignore an entire gesture.
上面的大概内容就是说,当你实现GestureDetector.OnGestureListener接口时,最好重写onDown() 方法,并且使其的返回值为true,这是因为所有手势都以onDown() 消息开头。
所以解决方法就是:
private class MyGestureListener : GestureDetector.SimpleOnGestureListener() {
override fun onDown(event: MotionEvent): Boolean {
return true
}
}