3
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
                   }
            }
     }
}

Here is my code. I would like to make a swipe calendar on android. However, Log always prints e1 as null at first 2-3 times. I had search for a solution. It suggested to add onDown method. I also added it but it wasn't work. Is there anything wrong?

Thank you Ning

ning
  • 31
  • 3

2 Answers2

1

SimpleOnGestureListener always returns false for all methods including onScroll. You should return true instead of super.onScroll(e1, e2, distanceX, distanceY);

Nino van Hooff
  • 3,677
  • 1
  • 36
  • 52
0

You can provide this function in your activity to solve this problem:

@Override
    public boolean dispatchTouchEvent(MotionEvent me) {
        this.detector.onTouchEvent(me);
        return super.dispatchTouchEvent(me);
    }
Bowdzone
  • 3,827
  • 11
  • 39
  • 52