在Android中处理一个投掷/滚动而不消耗MotionEvent
我的应用程序屏幕边上有一个ViewFlipper,它包含许多不同的视图,我希望用户能够通过向左滑动来消除此问题。 所以我做了平常的...
private class SwipeDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        //Dismiss view
    }
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }
}
...然后将ViewFlipper的onTouchListener设置为在SwipeDetector中调用onTouchEvent。 这一切都很好,但我注意到,因为它正在消耗所有进入ViewFlipper的触摸事件,所以当ViewFlipper本身时,我无法点击任何东西。 如果我不覆盖onDown,或者我使它返回false,那么TouchListener会忽略事件的其余部分,我不会得到这个结果。 如果我甚至可以在用户触摸ViewFlipper之后处理所有ACTION_MOVE事件,但甚至无法完成这些操作,我很高兴能够共同进行某种自定义轻扫检测。 有没有什么方法可以在不消耗onDown的情况下继续监听MotionEvent?
  什么适用于我扩展View.OnTouchListener而不是SimpleGestureListener ,并重写onTouch方法。  请把这个片段作为一个非常简单的(和可改进的)方法来获得水平滑动检测。 
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            downX = event.getX();
            return true;
        }
        case MotionEvent.ACTION_UP: {
            upX = event.getX();
            float deltaX = downX - upX;
            if (Math.abs(deltaX) > MIN_DISTANCE) {
                if (deltaX < 0) {
                    this.onLeftToRightSwipe();
                    return true;
                }
                if (deltaX > 0) {
                    this.onRightToLeftSwipe();
                    return true;
                }
                return true;
            }
            return true;
        }
    }
    return false;
}
你注意到了GestureDetector吗? 从API版本1开始可用。
http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html
处理一举一动和滚动手势。
链接地址: http://www.djcxy.com/p/91251.html上一篇: Handle a fling/scroll in Android without consuming the MotionEvent
下一篇: implelenting onUp event handler using SimpleOnGestureListener
