【问题标题】:Why Motionevent e1 in onFling method always null at first time swipe?为什么 onFling 方法中的 Motionevent e1 在第一次滑动时总是为空?
【发布时间】:2014-07-18 08:13:46
【问题描述】:
private final class SwipeGesture extends SimpleOnGestureListener {
        private final int swipeMinDistance;
        private final int swipeThresholdVelocity;
         private MotionEvent mLastOnDownEvent = null;

        public SwipeGesture(Context context) {
            final ViewConfiguration viewConfig = ViewConfiguration.get(context);
            swipeMinDistance = viewConfig.getScaledTouchSlop();
            swipeThresholdVelocity = viewConfig.getScaledMinimumFlingVelocity();
        }

         @Override
         public boolean onDown(MotionEvent e) {
             Log.w("test log","onDown");
             Log.w("onDown e",String.valueOf(e));
             //Android 4.0 bug means e1 in onFling may be NULL due to onLongPress eating it.
             mLastOnDownEvent = e;
             return true;
         } 

         @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return false;
            }

            @Override
            public void onShowPress(MotionEvent e) {

            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                    float distanceY) {
                return super.onScroll(e1, e2, distanceX, distanceY);
            }

            @Override
            public void onLongPress(MotionEvent e) {
            }



        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            Log.w("test log","onFling");
            Log.w("mindistance",String.valueOf(swipeMinDistance));
            if(e1==null){
                Log.w("test log","start null"); 
                e1=mLastOnDownEvent;
                if(mLastOnDownEvent!=null)Log.w("on down e1",String.valueOf(mLastOnDownEvent.getX()));  
                Toast.makeText(getActivity(), "start+finish=null "+String.valueOf(e2.getX()), Toast.LENGTH_SHORT).show();

            }else{
                Log.w("startx",String.valueOf(e1.getX()));
            }
            if(e2==null){
                Log.w("test log","finish null");

            }else{
                Log.w("finishx",String.valueOf(e2.getX()));
            }


            if(e1!=null && e2!=null){
                   if (e1.getX() - e2.getX() > swipeMinDistance){
                      //swipe right to left
                   }else if (e2.getX() - e1.getX() > swipeMinDistance){
                      //swipe left to right
                   }
            }
     }
}

这是我的代码。我想在android上制作一个滑动日历。但是,Log 总是在前 2-3 次将 e1 打印为 null。 我一直在寻找解决方案。它建议添加 onDown 方法。我也添加了它,但它不起作用。有什么问题吗?

谢谢 宁

【问题讨论】:

  • onDown 如果其他视图窃取触摸事件,则无济于事
  • 您使用什么视图附加此事件?

标签: android swipe onfling


【解决方案1】:

SimpleOnGestureListener 对于包括 onScroll 在内的所有方法总是返回 false。你应该返回 true 而不是 super.onScroll(e1, e2, distanceX, distanceY);

【讨论】:

    【解决方案2】:

    你可以在你的activity中提供这个函数来解决这个问题:

    @Override
        public boolean dispatchTouchEvent(MotionEvent me) {
            this.detector.onTouchEvent(me);
            return super.dispatchTouchEvent(me);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 2018-06-02
      • 1970-01-01
      相关资源
      最近更新 更多