2014-05-25 114 views
1

在下面的發佈代碼中,我試圖使用onDownonFling來檢測滑動,但是當我運行該應用程序時,什麼都不顯示。滑動未被檢測到

我希望看到輸出來自onFling當我持有觸摸屏幕並迅速拖過我的手指,但我什麼也沒收到。另外,當我按下屏幕時,我預計會看到來自onDown的輸出,但也沒有出現。

請讓我知道我在想什麼或者我誤以爲onFling的功能在onDown?!

JavaCode:

super.onCreate(savedInstanceState); 
    setContentView(R.layout.swipe_gesture_activivty); 

    mGestureDetector = new GestureDetector(getApplicationContext(), new MySwipeGestureDetector()); 

      mViewGestureDetector = new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      return mGestureDetector.onTouchEvent(event); 
     } 
    }; 
    final TextView view = (TextView) findViewById(R.id.accXLabel);  
} 

private class MySwipeGestureDetector extends SimpleOnGestureListener implements OnTouchListener { 

    @Override 
    public boolean onDown(MotionEvent e) { 
     // TODO Auto-generated method stub 
     Toast.makeText(getApplicationContext(), "onDown", Toast.LENGTH_LONG).show(); 
     return true; 
    } 

    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
      float velocityY) { 
     // TODO Auto-generated method stub 
     try { 
      if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
       return false; 
      // right to left swipe 
      if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show(); 
      } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show(); 
      } 
     } catch (Exception e) { 
      // nothing 
     } 
     return false; 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     // TODO Auto-generated method stub 
     return mGestureDetector.onTouchEvent(event); 
    } 

} 

}

+0

你不添加OnGestureListener任何看法,就張貼的代碼所示(?) – matiash

+0

將在短期內編輯...發佈錯誤代碼 – LetsamrIt

+0

@matiash請參閱更新代碼 – LetsamrIt

回答

1

首先,我假設一個例子,您在layout.xml文件中的所有視圖都包含在framelayout的內部,並且您希望在整個framelayout之間檢測到您的滑動。如果是這樣,註冊您的framelayout的類型GestureDetector的對象顯示在下面的代碼:

FrameLayout mFL = (FrameLayout) findViewById(R.id.framelayoutID); 

    mGestureDetector = new GestureDetector(getApplicationContext(), new MySwipeGestureDetector()); 
    mFL.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      return mGestureDetector.onTouchEvent(event); 
     } 
    }); 

關於simpleOnGestureListener,這裏是如何實現onFling。它爲我工作:

//define these variables globally: 
/* private static final int MIN_DIST = 100; 
private static final int MAX_OFF_PATH = 200; 
private static final int THRESHOLD_VELOC = 200;*/ 
... 
... 
... 
@Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
      float velocityY) { 
     // TODO Auto-generated method stub 
     try { 
      if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) 
       return false; 
      // right to left swipe 
      if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       Log.i("SwipeGesture", "Left Swipe"); 
      } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
       Log.i("SwipeGesture", "Right Swipe"); 
      } 
     } catch (Exception e) { 
      // nothing 
     } 
     return false; 
    }  
} 
1

GestureDetector一個作品由多個翻譯成MotionEvents特定手勢。爲此,它必須從視圖的觸摸事件接收輸入。

你只是缺少

view.setOnTouchListener(mViewGestureDetector); 

特定視圖(你想趕上甩上一個)。