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方法。我也添加了它,但它不起作用。有什麼不對的嗎?爲什麼Motionevent e1的onFling方法在第一次刷卡時始終爲空?
謝謝 寧
如果某些其他視圖是盜取觸摸事件,則onDown無助 – Blackbelt
您使用什麼視圖附加此事件? – Arst