Android爬坑记录——GestureDetector无效

本文详细解析了在Android开发中,使用GestureDetector时遇到无效情况的原因。关键在于重写onDown()方法并确保其返回true,以避免系统忽略后续手势事件,导致应用出现意外问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
        }
     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值