Skip to content Skip to sidebar Skip to footer

Swipt Detector Gestures Android

@Override public boolean onTouchEvent(MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { return true; } return super.onTouchEvent(event); } private

Solution 1:

You must apply the listener to the View you like it to work on, something like:

myView.setOnTouchListener(newSwipeGestureDetector(this)....

Solution 2:

Try updated code

publicclassOnSwipeTouchListenerimplementsOnTouchListener {
privatefinal GestureDetector gestureDetector;
intSWIPE_THRESHOLD=200;
intSWIPE_VELOCITY_THRESHOLD=200;

publicOnSwipeTouchListener(Context context) {
    gestureDetector = newGestureDetector(context, newCustomGestureListenerClass());
}

@OverridepublicbooleanonTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

privatefinalclassCustomGestureListenerClassextendsSimpleOnGestureListener {


    @OverridepublicbooleanonDown(MotionEvent e) {
        returnfalse;
    }

    @OverridepublicbooleanonSingleTapUp(MotionEvent e) {
        singleClicked(e);
        returnsuper.onSingleTapUp(e);
    }

    @OverridepublicbooleanonScroll(MotionEvent startMotionEvent, MotionEvent endMotionEvent, float distanceX, float distanceY) {
        returnsuper.onScroll(startMotionEvent, endMotionEvent, distanceX, distanceY);
    }


    @OverridepublicbooleanonFling(MotionEvent startMotionEvent, MotionEvent endMotionEvent, float velocityX, float velocityY) {

        booleanresult=false;
        try {
            floatdiffY= endMotionEvent.getY() - startMotionEvent.getY();
            floatdiffX= endMotionEvent.getX() - startMotionEvent.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
                result = true;
            }
            result = true;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

publicvoidonSwipeRight() {
    // if you want done some portion before call this method then write here
}

publicvoidonSwipeLeft() {
    // if you want done some portion before call this method then write here
}

publicvoidsingleClicked(MotionEvent e) {
}
}

Solution 3:

create OnSwipeTouchListener.java as external class for reusabale and other behavior as

publicclassOnSwipeTouchListenerimplementsView.OnTouchListener {

 private final GestureDetector gestureDetector;
 Context context;

 publicOnSwipeTouchListener(Context ctx,View mainView) {
gestureDetector = newGestureDetector(ctx, newGestureListener());
mainView.setOnTouchListener(this);
context = ctx;
 }

 @OverridepublicbooleanonTouch(View v, MotionEvent event) {
     return gestureDetector.onTouchEvent(event);
 }

 publicclassGestureListenerextendsGestureDetector.SimpleOnGestureListener {

privatestatic final int SWIPE_THRESHOLD = 100;
privatestatic final int SWIPE_VELOCITY_THRESHOLD = 100;

@OverridepublicbooleanonDown(MotionEvent e) {
    returntrue;
}

@OverridepublicbooleanonFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    boolean result = false;
    try {
        float diffY = e2.getY() - e1.getY();
        float diffX = e2.getX() - e1.getX();
        if (Math.abs(diffX) > Math.abs(diffY)) {
            if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) >      SWIPE_VELOCITY_THRESHOLD) {
                if (diffX > 0) {
                    onSwipeRight();
                } else {
                    onSwipeLeft();
                }
                result = true;
            }
        } elseif (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
            if (diffY > 0) {
                onSwipeBottom();
            } else {
                onSwipeTop();
            }
            result = true;
        }
    } catch (Exception exception) {
        exception.printStackTrace();
    }
    return result;
     }
 }

 publicvoidonSwipeRight() {
Toast.makeText(context, "Right", Toast.LENGTH_SHORT).show();
this.onSwipe.swipeRight();
 }

 publicvoidonSwipeLeft() {
Toast.makeText(context, "Left", Toast.LENGTH_SHORT).show();
this.onSwipe.swipeLeft();
 }

 publicvoidonSwipeTop() {
 }

 publicvoidonSwipeBottom() {
 }

 interface onSwipeListener {
voidswipeRight();

voidswipeLeft();
 }

 ;
 onSwipeListener onSwipe;

 publicvoidsetOnSwipeListener(onSwipeListener onSwipeListener) {
this.onSwipe = onSwipeListener;
 }
 }

and in your main activity use it

OnSwipeTouchListener onSwipeTouchListener = newOnSwipeTouchListener(this,           findViewById(R.id.main_view));
     onSwipeTouchListener.setOnSwipeListener(newOnSwipeTouchListener.onSwipeListener() {
         @OverridepublicvoidswipeRight() {

    }

    @OverridepublicvoidswipeLeft() {

    }
     });

in your activity_main layout set an id to your main layout like main_view

have nice coding ...

Post a Comment for "Swipt Detector Gestures Android"