【发布时间】:2019-04-22 14:20:56
【问题描述】:
我已将我的 Activity 设置为具有检测左右滑动的手势检测器Compat,但我的 Activity 中也有一个水平的 RecyclerView。
当我尝试滚动 RecyclerView Activity 时,getsureDetector 也会触发,这是不可取的。
如果有帮助,这里是与gestureDetector相关的代码。
private GestureDetectorCompat mDetector;
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
mDetector = new GestureDetectorCompat(this, new SwipeDetector());
public boolean onTouchEvent(MotionEvent event){
this.mDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
public boolean dispatchTouchEvent(MotionEvent ev){
if (mDetector != null){
if (mDetector.onTouchEvent(ev)){
return true;
}
}
return super.dispatchTouchEvent(ev);
}
public class SwipeDetector extends GestureDetector.SimpleOnGestureListener{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH){
return false;
}
if( e2.getX() > e1.getX() ){
if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY){
Left();
return true;
}
}
if( e1.getX() > e2.getX() ){
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY){
Right();
return true;
}
}
return false;
}
}
【问题讨论】:
标签: java android gesturedetector