天天看點

Android爬坑記錄——GestureDetector無效

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