Android:错误的监听器行为?

时间:2012-03-09 12:55:28

标签: android onclick listener gesture swipe

我开始使用android开发。我想做一个日历活动。我有一个活动,它使用OnGestureListener等方法实现onFling(),以便detect swipe gestures,并更改日历的月份。这很好用。

之后,我的日历有几个单元格,每天一个。我在每个单元格中添加了一个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);
}

1 个答案:

答案 0 :(得分:0)

在使用单元格时,看起来使用GestureListener的View正在失去焦点。完成单元格后,只需尝试使用calendarCellView.clearFocus();