Created
August 18, 2019 18:50
-
-
Save slavkoder/2942f0d36c1d460dcaa6e0a10bc19dd4 to your computer and use it in GitHub Desktop.
Swipe detector class that supports AndroidX (copied from https://gist.github.com/rogerhu/9e769149f9550c0a6ddb4987b94caee8)
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
import androidx.recyclerview.widget.RecyclerView; | |
public class RecyclerViewSwipeListener extends RecyclerView.OnFlingListener { | |
private static final int SWIPE_VELOCITY_THRESHOLD = 2000; | |
boolean mIsScrollingVertically; | |
// change swipe listener depending on whether we are scanning items horizontally or vertically | |
RecyclerViewSwipeListener(boolean vertical) { | |
mIsScrollingVertically = vertical; | |
} | |
@Override | |
public boolean onFling(int velocityX, int velocityY) { | |
if (mIsScrollingVertically && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { | |
if (velocityY < 0) { | |
onSwipeDown(); | |
} else { | |
onSwipeUp(); | |
} | |
return true; | |
} else if (!mIsScrollingVertically && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { | |
if (velocityX < 0) { | |
onSwipeLeft(); | |
} else { | |
onSwipeRight(); | |
} | |
return true; | |
} | |
return false; | |
} | |
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