2014-07-18 73 views
2
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方法在第一次刷卡時始終爲空?

謝謝 寧

+0

如果某些其他視圖是盜取觸摸事件,則onDown無助 – Blackbelt

+0

您使用什麼視圖附加此事件? – Arst

回答

0

您可以在您的活動提供這個功能來解決這個問題:

@Override 
    public boolean dispatchTouchEvent(MotionEvent me) { 
     this.detector.onTouchEvent(me); 
     return super.dispatchTouchEvent(me); 
    } 
0

SimpleOnGestureListener總是返回所有方法,包括onScroll假。你應該返回true而不是super.onScroll(e1,e2,distanceX,distanceY);

相關問題