Skip to content

Instantly share code, notes, and snippets.

@marvinlix
Forked from rogerhu/RecyclerViewSwipeListener
Created August 25, 2016 06:18
Show Gist options
  • Select an option

  • Save marvinlix/8fdc66cbda2e70042c50de4cd4dd3eb9 to your computer and use it in GitHub Desktop.

Select an option

Save marvinlix/8fdc66cbda2e70042c50de4cd4dd3eb9 to your computer and use it in GitHub Desktop.
import android.support.v7.widget.RecyclerView;
public class RecyclerViewSwipeListener extends RecyclerView.OnFlingListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
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