在下面的發佈代碼中,我試圖使用onDown
和onFling
來檢測滑動,但是當我運行該應用程序時,什麼都不顯示。滑動未被檢測到
我希望看到輸出來自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);
}
}
}
你不添加OnGestureListener任何看法,就張貼的代碼所示(?) – matiash
將在短期內編輯...發佈錯誤代碼 – LetsamrIt
@matiash請參閱更新代碼 – LetsamrIt