public class GameActivity extends Activity {
private static final String TAG = "GameActivity";
. . .
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "+ onTouchEvent(event:" + event + ")");
Log.d(TAG, "- onTouchEvent()");
return super.onTouchEvent(event);
}
. . .
}
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "GameView";
. . .
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d(TAG, "+ onTouchEvent(event:" + event + ")");
gestureDetector.onTouchEvent(event);
Log.d(TAG, "- onTouchEvent()");
return super.onTouchEvent(event);
}
private SimpleOnGestureListener gestureListener = new SimpleOnGestureListener() {
private static final String TAG = "GestureListener";
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d(TAG, "+ onSingleTapConfirmed(event:" + event + ")");
singleTapDetected = true;
Log.d(TAG, "- onSingleTapConfirmed()");
return true;
}
@Override
public boolean onDoubleTap(MotionEvent event) {
Log.d(TAG, "+ onDoubleTap(event:" + event + ")");
doubleTapDetected = true;
Log.d(TAG, "- onDoubleTap()");
return true;
}
};
private GestureDetector gestureDetector = new GestureDetector(getContext(), gestureListener);
. . .
}
I ran it on AVD an made a single click on the SurfaceView (GameView).
According to LogCat logs:
03-11 14:19:51.171: D/GameView(4839): + onTouchEvent(event:MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=1071.0, y[0]=437.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=27223172, downTime=27223172, deviceId=0, source=0x1002 })
03-11 14:19:51.171: D/GameView(4839): - onTouchEvent()
03-11 14:19:51.171: D/GameActivity(4839): + onTouchEvent(event:MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=1071.0, y[0]=437.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=27223172, downTime=27223172, deviceId=0, source=0x1002 })
03-11 14:19:51.171: D/GameActivity(4839): - onTouchEvent()
03-11 14:19:51.299: D/GameActivity(4839): + onTouchEvent(event:MotionEvent { action=ACTION_UP, id[0]=0, x[0]=1071.0, y[0]=437.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=27223303, downTime=27223172, deviceId=0, source=0x1002 })
03-11 14:19:51.299: D/GameActivity(4839): - onTouchEvent()
There are several questions:
Why the
SimpleOnGestureListener
was not called? (Even thoughonTouchEvent()
was called and presummably the linegestureDetector.onTouchEvent(event);
was executed)Why the GameActivity's
onTouchEvent()
gets ACTION_DOWN and ACTION_UP, but the GameView'sonTouchEvent()
only gets ACTION_DOWN?
Some things I have tried
I implemented the
GestureDetector
on theGameActivity
class with the same code as shown above and it worked as expected, callingonSingleTapConfirmed()
In GameView, instead of a private member
SimpleOnGestureListener
, created a privateclass GestureListener extends SimpleOnGestureListener
and used it to construct theGestureDetector
. I see the same behavior as described, no call to the listener.