根据Android: How to handle right to left swipe gestures中的代码,我为GestureDetector
创建了以下ListView
以处理滑动。
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;
}
摘要GestureDetector
看起来像
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();
}
使用print
中的OnFling
语句,我发现onFling
以非常不一致的方式被调用;只有大约一半的滑动触发了要调用的功能。如果在调用该函数时,它能够区分onSwipeRight()
和onSwipeLeft()
就好了。禁用长时间点击似乎有点帮助,但不清楚为什么onFling
只会不时触发,也许MotionEvents
会被某个地方消耗掉?