我在RecyclerView项目中有一个视频播放器,文本,图像(想想社交媒体帖子)。每当用户在视频上向左或向右滑动时,我都想向前或向后搜索视频。 通过将以下触摸侦听器设置为我的视频播放器视图,我实现了这一点:
public abstract class OnSwipeTouchListener implements View.OnTouchListener {
private final GestureDetector gestureDetector;
public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
}
public abstract void onSwipeLeft();
public abstract void onSwipeRight();
public abstract void onSingleTap();
@Override
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 onSingleTapUp(MotionEvent e) {
onSingleTap();
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(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}
}
}
这并不总是作为recyclerview工作,或者有什么东西妨碍我,只要我轻扫,只有onDown会在我的监听器和recyclerview滚动中调用。
如果没有其他视图拦截它,我怎样才能让我的视频播放器先获得动作?