Android GestureDetector with SimpleOnGestureListener within SurfaceView

余生长醉 提交于 2019-12-05 12:08:57

I debugged the code into the android source code, GestureDetector class. And this explains why SimpleOnGestureListener was not called:

  • The ACTION_DOWN is received and generates an internal TAP message to the GestureDetector message Handler.
  • The Handler calls the listener onSingleTapConfirmed() only if the event is not still down (!mStillDown)
  • The event is still down, because the ACTION_UP event was never received.

So you see, the two questions are related, when I find out why ACTION_UP is not issued I will solve the problem!

Edit

This completes the answer, now it is working.

  • The call to super.onTouchEvent() in GameView returns false, because the super class of SurfaceView is View, and it returns false. That's why ACTION_UP is never called
  • The call to super.onTouchEvent() in GameActivity returns true, that's why ACTION_UP is issued on GameActivity.

Note: If I set GameView.setclickable(true) then super.onTouchEvent() returns true. It is also an acceptable solution

Now my GameView code is:

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d(TAG, "+ onTouchEvent(event:" + event + ")");
    gestureDetector.onTouchEvent(event);
    Log.d(TAG, "- onTouchEvent()");
    return true;
}

The current LogCat logs are:

03-11 16:32:06.629: D/GameView(5316): + onTouchEvent(event:MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=977.0, y[0]=414.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=35158623, downTime=35158623, deviceId=0, source=0x1002 })
03-11 16:32:06.629: D/GameView(5316): - onTouchEvent()
03-11 16:32:06.641: D/GameView(5316): + onTouchEvent(event:MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=978.0, y[0]=414.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=35158623, downTime=35158623, deviceId=0, source=0x1002 })
03-11 16:32:06.641: D/GameView(5316): - onTouchEvent()
03-11 16:32:06.772: D/GameView(5316): + onTouchEvent(event:MotionEvent { action=ACTION_UP, id[0]=0, x[0]=978.0, y[0]=414.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=35158772, downTime=35158623, deviceId=0, source=0x1002 })
03-11 16:32:06.772: D/GameView(5316): - onTouchEvent()
03-11 16:32:06.931: D/GestureListener(5316): + onSingleTapConfirmed(event:MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=977.0, y[0]=414.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=35158623, downTime=35158623, deviceId=0, source=0x1002 })
03-11 16:32:06.931: D/GestureListener(5316): - onSingleTapConfirmed()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!