private final class SwipeGesture extends SimpleOnGestureListener {
private final int swipeMinDistance;
private final int swipeThresholdVelocity;
private MotionEvent mLastOnDownEvent = null;
public SwipeGesture(Context context) {
final ViewConfiguration viewConfig = ViewConfiguration.get(context);
swipeMinDistance = viewConfig.getScaledTouchSlop();
swipeThresholdVelocity = viewConfig.getScaledMinimumFlingVelocity();
}
@Override
public boolean onDown(MotionEvent e) {
Log.w("test log","onDown");
Log.w("onDown e",String.valueOf(e));
//Android 4.0 bug means e1 in onFling may be NULL due to onLongPress eating it.
mLastOnDownEvent = e;
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
Log.w("test log","onFling");
Log.w("mindistance",String.valueOf(swipeMinDistance));
if(e1==null){
Log.w("test log","start null");
e1=mLastOnDownEvent;
if(mLastOnDownEvent!=null)Log.w("on down e1",String.valueOf(mLastOnDownEvent.getX()));
Toast.makeText(getActivity(), "start+finish=null "+String.valueOf(e2.getX()), Toast.LENGTH_SHORT).show();
}else{
Log.w("startx",String.valueOf(e1.getX()));
}
if(e2==null){
Log.w("test log","finish null");
}else{
Log.w("finishx",String.valueOf(e2.getX()));
}
if(e1!=null && e2!=null){
if (e1.getX() - e2.getX() > swipeMinDistance){
//swipe right to left
}else if (e2.getX() - e1.getX() > swipeMinDistance){
//swipe left to right
}
}
}
}
这是我的代码。我想在android上制作一个滑动日历。但是,Log始终首先将e1打印为空值2-3次。 我找到了解决方案。它建议添加onDown方法。我也加了它,但它没有用。有什么不对吗?
谢谢 宁
答案 0 :(得分:1)
SimpleOnGestureListener始终为包括onScroll在内的所有方法返回false。你应该返回true而不是super.onScroll(e1,e2,distanceX,distanceY);
答案 1 :(得分:0)
您可以在活动中提供此功能来解决此问题:
@Override
public boolean dispatchTouchEvent(MotionEvent me) {
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}