2013-08-05 42 views
1

我正在使用GestureDetector來調用onFling()。它似乎正確地檢測我的閃光,因爲它觸發我創建的日誌消息。我試圖確定投擲方向,但遇到問題。對於傳遞給onFling()方法的兩個MotionEvent對象,x值是相同的,因此我無法確定方向。例如,我得到:確定滑動方向

08-05 16:36:08.679: DEBUG/mView(14616): fling2: 131.0 131.0 

當我這樣做:

Log.d("mView", "fling2: " + e1.getX() + " " + e2.getX()); 

在進行一扔,我只是提出我的手指橫所以這是沒有意義的我。這裏可能會出現什麼問題?

+0

什麼'Y'?它會改變嗎? – Desert

+0

@ user1873880 Y確實發生了變化。如果我製作一個類似的調試日誌,我會在執行對角線投射時獲得'fling3:775.0 599.0',但對於X來說仍然是'fling2:167.0 167.0'。 –

+0

@ trevor-e droidQuery應該可以幫助您。創建者是phil brown,他也有一個堆棧溢出帳戶。如果您有關於droidQuery的任何問題,請隨時詢問droidQuery標籤,以便我或他可以輕鬆找到您的問題。 – superuser

回答

2

您可以使用droidQuery:https://github.com/phil-brown/droidQuery。它會真正簡化您的代碼並使其易於使用。這裏是所有你需要把你的活動onCreate():

//global variables 
private boolean isSwiping = false; 
private SwipeDetector.Direction swipeDirection = null; 
private View v;//set to the parent layout of the fragments. 

//swipe-handling code 
$.with(v).swipe(new Function() { 
    @Override 
    public void invoke($ droidQuery, Object... params) { 
     if (params[0] == SwipeDetector.Direction.START) 
      isSwiping = true; 
     else if (params[0] == SwipeDetector.Direction.STOP) { 
      if (isSwiping) { 
       isSwiping = false; 
       if (swipeDirection != null) { 
        switch(swipeDirection) { 
         case DOWN : 
          //TODO: Down swipe complete, so do something 
          break; 
         case UP : 
          //TODO: Up swipe complete, so do something 
          break; 
         case LEFT : 
          //TODO: Left swipe complete, so do something 
          break; 
         case RIGHT : 
          //TODO: Right swipe complete, so do something (such as): 
          day++; 
          Fragment1 rightFragment = new Fragment1(); 
          Bundle args = new Bundle(); 
          args.putInt("day", day); 
          rightFragment.setArguments(args); 

          android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
          transaction.replace(R.id.fragment_container, rightFragment); 
          transaction.addToBackStack(null); 
          transaction.commit(); 
          break; 
         default : 
          break; 
        } 
       } 
      } 
     } 
     else { 
      swipeDirection = (SwipeDetector.Direction) params[0]; 
     } 
    } 
}); 
+0

對不起,這個圖書館看起來不錯,但它不是我所需要的。我在想我的問題是更多的錯誤相關。 –