我需要一个日历网格视图上的触摸监听器。它应该有一个 onTouch 方法来从日历上拖动获取数据,以及一个 onDoubleTapEvent 来删除条目。我还实现了自定义类 MyGestureListener,它扩展了 SimpleOnGestureListener 来执行此操作。部分代码如下所示:
calendarGridView.setOnTouchListener(new MyGestureListener(getApplicationContext()) {
//Touch Listener on every gridcell
public boolean onTouch(View v, MotionEvent event) {
super.onTouch(v, event);
....
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
....
}
case MotionEvent.ACTION_UP : {
....
}
case MotionEvent.ACTION_CANCEL: {
....
}
}
//Save the data
return true;
}
public boolean onDoubleTapEvent(MotionEvent event) {
.... //delete the entry, save data
return true;
}
自定义手势监听类:
public class MyGestureListener extends SimpleOnGestureListener implements OnTouchListener{
Context context;
GestureDetector gDetector;
public MyGestureListener(Context context) {
super();
if (gDetector == null) {
gDetector = new GestureDetector(context, this);
}
this.context = context;
}
public MyGestureListener(Context context, GestureDetector gDetector) {
if (gDetector == null) {
gDetector = new GestureDetector(context, this);
}
this.context = context;
this.gDetector = gDetector;
}
public boolean onTouch(View v, MotionEvent event) {
return gDetector.onTouchEvent(event);
}
public GestureDetector getDetector() {
return gDetector;
}
}
这里的问题是,当我双击日历单元格时,它会调用 onDoubleTapEvent,但也会调用 onTouch 方法(考虑 ACTION_DOWN、ACTION_UP 和 ACTION_DOWN、ACTION_UP)。我怎样才能将它们分开?