Created
January 8, 2021 07:40
-
-
Save thrashedbrain/7172f35b198474c167379a4a5403e18c 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
public class SwipeGestureDetector implements View.OnTouchListener { | |
private GestureDetector gestureDetector; | |
public SwipeGestureDetector(Context context) { | |
gestureDetector = new GestureDetector(context, new GestureListener()); | |
} | |
public SwipeGestureDetector() { | |
} | |
public void onSwipeUp() { | |
} | |
public void onSwipeDown() { | |
} | |
public boolean onTouch(View v, MotionEvent event) { | |
return gestureDetector.onTouchEvent(event); | |
} | |
private final class GestureListener extends GestureDetector.SimpleOnGestureListener { | |
private static final int SWIPE_DISTANCE_THRESHOLD = 100; | |
private static final int SWIPE_VELOCITY_THRESHOLD = 100; | |
@Override | |
public boolean onDown(MotionEvent e) { | |
return true; | |
} | |
@Override | |
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { | |
float distanceX = e2.getX() - e1.getX(); | |
float distanceY = e2.getY() - e1.getY(); | |
if (Math.abs(distanceY) > Math.abs(distanceX) && Math.abs(distanceY) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { | |
if (distanceY > 0) | |
onSwipeDown(); | |
else | |
onSwipeUp(); | |
return true; | |
} | |
return false; | |
} | |
} | |
} |
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
linearLayout.setOnTouchListener(new HoroscopeGestureDetector(context){ | |
@Override | |
public void onSwipeDown() { | |
//do something | |
super.onSwipeDown(); | |
} | |
@Override | |
public void onSwipeUp() { | |
//do something | |
super.onSwipeUp(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment