Based on the code from Android: How to handle right to left swipe gestures, I've created the following GestureDetector
for my ListView
in order to handle swipes.
public ListEntryViewHolder(View view) {
ButterKnife.bind(this, view);
mGestureDetector = new GestureDetector(view.getContext(), new ListSwipeListener(view));
}
@OnTouch(R.id.rl_list_entry)
boolean ListTouch(View ListEntry, MotionEvent event) {
mGestureDetector.onTouchEvent(event);
return false;
}
The abstract GestureDetector
looks like
public abstract class LeftRightSwipeListener extends SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 5;
private static final int SWIPE_VELOCITY_THRESHOLD = 5;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// Print Statement
// Swipe Based Code that does some math and calls onSwipeRight() and onSwipeLeft()
}
public abstract void onSwipeRight();
public abstract void onSwipeLeft();
}
Using the print
statement within OnFling
, I've found that onFling
is being called in a very inconsistent manner; only about half of my swipes trigger the function to be called. If and when the function is called, it's able to distinguish between onSwipeRight()
and onSwipeLeft()
just fine. Disabling long clicks seemed to help a little, but it's unclear why onFling
only triggers from time to time, perhaps the MotionEvents
are getting consumed somewhere?