我正在尝试在recycleview上实现向左滑动和向内移动Pinch。在recycleview上实现(滑动和捏合)触摸侦听器之后。它表现得很奇怪。大多数时候onFling()方法都有e1 = null。即便捏也不那么完美。在捏的时候,它主要是滚动和捏不起作用。 这是我的代码:
mScaleDetector = new ScaleGestureDetector(getActivity(), new ScaleListener());
mGestureDetector = new GestureDetector(getActivity(), new GestureListener());
recycleview.setOnTouchListener((v, event) -> {
if (mGestureDetector.onTouchEvent(event)) {
return false;
} else if (mScaleDetector.onTouchEvent(event)) {
return false;
}
return true;
});
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
private float mScaleFactor = 1.0f;
@Override
public boolean onScale(ScaleGestureDetector detector) {
super.onScale(detector);
mScaleFactor = detector.getScaleFactor();
return true;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
super.onScaleBegin(detector);
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
super.onScaleEnd(detector);
if (mScaleFactor > 1) {
//Toast.makeText("Zooming Out");
} else if (mScaleFactor < 1) {
//((AllShowsFragmentView) mContext).pinchInward();
//Toast.makeText("Zooming In");
moveToHomeScreen();
}
}
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
protected MotionEvent mLastOnDownEvent = null;
/* @Override
public boolean onSingleTapUp(MotionEvent e)
{
//single tap returns true so we know to register it over a swipe
//onSingleTap();
return false;
}*/
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
//onSingleTap();
return super.onSingleTapConfirmed(e);
}
@Override
public boolean onDown(MotionEvent e) {
mLastOnDownEvent = e;
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
if (e1==null)
e1 = mLastOnDownEvent;
if (e1==null || e2==null)
return false;
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
//onSwipeRight();
} else {
//onSwipeLeft();
moveToHomeScreen();
}
result = false;
}
} else if (Math.abs(diffY) > SWIPE_THRESHOLD
&& Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
//onSwipeBottom();
} else {
//onSwipeTop();
}
result = false;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}