-
-
Save d0minicw0ng/d77defdb988408b34bd0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Usage: | |
myView.setOnTouchListener(new OnSwipeTouchListener(this) { | |
@Override | |
public void onSwipeDown() { | |
Toast.makeText(MainActivity.this, "Down", Toast.LENGTH_SHORT).show(); | |
} | |
} | |
*/ | |
public class OnSwipeTouchListener implements OnTouchListener { | |
private GestureDetector gestureDetector; | |
public OnSwipeTouchListener(Context c) { | |
gestureDetector = new GestureDetector(c, new GestureListener()); | |
} | |
public boolean onTouch(final View view, final MotionEvent motionEvent) { | |
return gestureDetector.onTouchEvent(motionEvent); | |
} | |
private final class GestureListener extends SimpleOnGestureListener { | |
private static final int SWIPE_THRESHOLD = 100; | |
private static final int SWIPE_VELOCITY_THRESHOLD = 100; | |
@Override | |
public boolean onDown(MotionEvent e) { | |
return true; | |
} | |
// Determines the fling velocity and then fires the appropriate swipe event accordingly | |
@Override | |
public boolean onFling(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(); | |
} | |
} | |
} else { | |
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { | |
if (diffY > 0) { | |
onSwipeDown(); | |
} else { | |
onSwipeUp(); | |
} | |
} | |
} | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
} | |
return result; | |
} | |
} | |
public void onSwipeRight() { | |
} | |
public void onSwipeLeft() { | |
} | |
public void onSwipeUp() { | |
} | |
public void onSwipeDown() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment