我從android開發開始。我想做一個日曆活動。我有一個活動,它實現了OnGestureListener
,方法如onFling()
,以便detect swipe gestures
,並更改日曆的月份。這工作正常。Android:錯誤的偵聽器行爲?
之後,我的日曆有幾個單元格,每天一個。我爲每個單元添加了一個onclick
偵聽器,以便爲今天添加事件。當我這樣做時,我的gestureListener
停止工作,並且只捕獲單元格上的單擊事件,但是滑動手勢不起作用(方法從未被調用過(onFling()
))。我能做什麼?
這是我的代碼:
public class Calendar extends Activity implements OnGestureListener{ ...
//OnFling method that detects the swipe gesture
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
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();
}
else if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(getApplicationContext(), "Swipe up", Toast.LENGTH_SHORT).show();
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(getApplicationContext(), "Swipe down", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(this, "exception", Toast.LENGTH_SHORT).show();
}
return true;
}
//This is the method attached to each cell for onclick event
public void showDialog(View v){
TextView dia = (TextView)v.findViewWithTag("dia");
Intent i = new Intent(this, DialogoCalendario.class);
i.putExtra("dia",(String) dia.getText());
i.putExtra("mes", ((TextView) findViewById(R.id.tbMes)).getText().toString());
startActivity(i);
}
顯示您的代碼並查看以下鏈接https://www.google.co.in/search?q=detect+swipe+gestures+android&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en -US:official&client = firefox-a – Shruti 2012-03-09 13:12:09
我更新了我的帖子,代碼爲 – user1116714 2012-03-09 13:47:42