我正在使用onTouch
方法与ACTION_UP
和GestureDetector
进行联系以捕获双击,我的问题是双击结果点击然后双击,然后轻拍。有没有办法让双击阻止水龙头或类似的东西?我从逻辑上知道它的行为是正确的,所以如果你建议我找另一种方式来评论,请不要投票。谢谢!
答案 0 :(得分:5)
我建议您切换到SimpleGestureListener并使用onDoubleTap()和onSingleTapConfirmed()方法。
答案 1 :(得分:2)
为了比britzl所说的更精确,GestureDetector
执行确定何时单击,双击,长按等的实际工作。{{1} }只是SimpleGestureListener
用来表示识别内容的“倾听者”。它实现GestureDetector
和OnGestureListener
只是为了总是返回false。查看OnDoubleTapListener
中onTouchEvent(MotionEvent)
的摘要:
GestureDetector
然后可以通过使用适当的侦听器创建case MotionEvent.ACTION_DOWN:
if (mDoubleTapListener != null) {
boolean hadTapMessage = mHandler.hasMessages(TAP);
if (hadTapMessage) mHandler.removeMessages(TAP);
if ((mCurrentDownEvent != null) && (mPreviousUpEvent != null) && hadTapMessage &&
isConsideredDoubleTap(mCurrentDownEvent, mPreviousUpEvent, ev)) {
// This is a second tap
mIsDoubleTapping = true;
// Give a callback with the first tap of the double-tap
handled |= mDoubleTapListener.onDoubleTap(mCurrentDownEvent);
// Give a callback with down event of the double-tap
handled |= mDoubleTapListener.onDoubleTapEvent(ev);
} else {
// This is a first tap
mHandler.sendEmptyMessageDelayed(TAP, DOUBLE_TAP_TIMEOUT);
}
}
mDownFocusX = mLastFocusX = focusX;
mDownFocusY = mLastFocusY = focusY;
if (mCurrentDownEvent != null) {
mCurrentDownEvent.recycle();
}
mCurrentDownEvent = MotionEvent.obtain(ev);
mAlwaysInTapRegion = true;
mAlwaysInBiggerTapRegion = true;
mStillDown = true;
mInLongPress = false;
mDeferConfirmSingleTap = false;
if (mIsLongpressEnabled) {
mHandler.removeMessages(LONG_PRESS);
mHandler.sendEmptyMessageAtTime(LONG_PRESS, mCurrentDownEvent.getDownTime()
+ TAP_TIMEOUT + LONGPRESS_TIMEOUT);
}
mHandler.sendEmptyMessageAtTime(SHOW_PRESS, mCurrentDownEvent.getDownTime() + TAP_TIMEOUT);
handled |= mListener.onDown(ev);
break;
来获取所需的结果:
GestureDetector
从那里开始,final View.OnTouchListener touch_listener = new View.OnTouchListener() {
@Override public boolean onTouch(View view, MotionEvent event) {
return _gesture_detector.onTouchEvent(event);
}
private final GestureDetector _gesture_detector = new GestureDetector(getContext()
, new GestureDetector.SimpleOnGestureListener() {
@Override public boolean onSingleTapConfirmed(MotionEvent event) {
// TODO: implement single tap behavior
// NOTE: returning true indicates that the gesture was handled
return true;
}
@Override public boolean onDoubleTap(MotionEvent event) {
// TODO: implement double tap behavior
// NOTE: returning true indicates that the gesture was handled
return true;
}
});
};
可以设置为想要行为的OnTouchListener
。
它使用默认的View
(GestureHandler
}:
Handler
从private class GestureHandler extends Handler {
GestureHandler() {
super();
}
GestureHandler(Handler handler) {
super(handler.getLooper());
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_PRESS:
mListener.onShowPress(mCurrentDownEvent);
break;
case LONG_PRESS:
dispatchLongPress();
break;
case TAP:
// If the user's finger is still down, do not count it as a tap
if (mDoubleTapListener != null) {
if (!mStillDown) {
mDoubleTapListener.onSingleTapConfirmed(mCurrentDownEvent);
} else {
mDeferConfirmSingleTap = true;
}
}
break;
default:
throw new RuntimeException("Unknown message " + msg); //never
}
}
}
回忆mHandler.sendEmptyMessageDelayed(TAP, DOUBLE_TAP_TIMEOUT);
行。它延迟了有效双击手势的超时时段的敲击通知。来自GestureDetector
的行if (hadTapMessage) mHandler.removeMessages(TAP);
会在有效双击时删除该通知。 GestureDetector
在延迟后收到点击通知,并使用回调通过GestureHandler
通知GestureListener
。 mDoubleTapListener.onSingleTapConfirmed(mCurrentDownEvent);
会在收到点击通知时,如果用户的手指停止,则会将该回调延迟至GestureHandler
(由MotionEvent.ACTION_UP
处理)。
答案 2 :(得分:0)
除了布里茨建议你可能想要在那里检查你的逻辑一秒钟。
我不认为双击导致多重,它只会导致4个事件,就像你有点提到的那样。虽然Gesture库(我认为)是最佳选择,但您应该考虑:
这就是手势听众所做的事情