GestureDetector使用方法

来源:互联网 发布:中国第一程序员 编辑:程序博客网 时间:2024/05/16 23:49

首先,需要创建一个GestureDetector对象并实现OnGestureListener接口。

    GestureDetector detector = new GestureDetector(this);    //解决长按屏幕后无法拖动的现象    detector.setIsLongpressEnabled(false);

接着,接管目标View的onTouchEvent方法,在待监听View的onTouchEvent方法中添加如下实现:

    return detector.onTouchEvent(event);

然后,就可以有选择地实现OnGestureListener和OnDoubleTapListener中的方法了。


onDown

onShowPress
*和onDown区别在于强调的是没有松开或者拖动的状态

onSingleTapUp

onScroll

onLongPress

onFling

onDoubleTap

onSingleTapConfirmed
*和onSingleTapUp的区别是后面不可能再紧跟着另一个单击行为,既这只可能是单击,不可能是双击中的一次单击

onDoubleTapEvent


建议:如果只是监听滑动相关的,建议自己在onTouchEvent中实现,如果要监听双击这种行为的话,那么就使用GestureDetector。

原创粉丝点击