[GestureDetector.OnGestureListener][1] 类具有方法 [onSingleTapUp(MotionEvent)][2]:
Notified When a tap 发生与 up MotionEvent que 触发它。
这个方法和onclick的功能一样吗?我可以使用这个方法来实现我想要的 onclick 方法的相同行为吗?
我做了一些测试,似乎答案是肯定的,你可以使用 onSingleTapUp 而不是 onclick。onSingleTapUp 方法可以很好地区分双击或长按的简单单击。我的测试:
public class MainFragmentScrollerController
{
private MainFragment mFragment;
private GestureDetectorCompat mGestureDetector;
public MainFragmentScrollerController(MainFragment fragment)
{
mFragment = fragment;
mGestureDetector = new GestureDetectorCompat(mFragment.getActivity(), new GestureListener());
}
public void setGestureListener()
{
mFragment.myView.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
boolean gestureResult = mGestureDetector.onTouchEvent(event);
Log.i("GestureListener", "gestureResult = " + gestureResult);
return gestureResult;
}
});
}
public class GestureListener extends SimpleOnGestureListener
{
private final String TAG = "GestureListener";
@Override
public boolean onDown(MotionEvent event)
{
Log.d(TAG,"onDown: " + event.toString());
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY)
{
Log.d(TAG, "onFling: " + event1.toString()+event2.toString());
return true;
}
@Override
public boolean onScroll(MotionEvent event1, MotionEvent event2, float distanceX, float distanceY)
{
Log.d(TAG, "onScroll: " + event1.toString()+event2.toString());
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent event)
{
Log.d(TAG, "onSingleTapUp: " + event.toString());
return true;
}
@Override
public void onLongPress(MotionEvent event)
{
Log.d(TAG, "onLongPress: " + event.toString());
}
}
}
简单点击的结果:
09-04 14:20:58.040: D/GestureListener(17689): onDown: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=188.71875, y[0]=26.0625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34463725, downTime=34463725, deviceId=1, source=0x1002 }
09-04 14:20:58.040: I/GestureListener(17689): gestureResult = true
09-04 14:20:58.100: I/GestureListener(17689): gestureResult = false
09-04 14:20:58.130: D/GestureListener(17689): onSingleTapUp: MotionEvent { action=ACTION_UP, id[0]=0, x[0]=188.71875, y[0]=26.0625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34463811, downTime=34463725, deviceId=1, source=0x1002 }
09-04 14:20:58.130: I/GestureListener(17689): gestureResult = true
长按的结果:
09-04 14:22:12.520: D/GestureListener(17689): onDown: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=179.22656, y[0]=41.0625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34538199, downTime=34538199, deviceId=1, source=0x1002 }
09-04 14:22:12.520: I/GestureListener(17689): gestureResult = true
09-04 14:22:12.560: I/GestureListener(17689): gestureResult = false
09-04 14:22:12.930: I/GestureListener(17689): gestureResult = false
09-04 14:22:12.950: I/GestureListener(17689): gestureResult = false
09-04 14:22:13.010: I/GestureListener(17689): gestureResult = false
09-04 14:22:13.100: I/GestureListener(17689): gestureResult = false
09-04 14:22:13.130: I/GestureListener(17689): gestureResult = false
09-04 14:22:13.150: I/GestureListener(17689): gestureResult = false
09-04 14:22:13.180: I/GestureListener(17689): gestureResult = false
09-04 14:22:13.200: D/GestureListener(17689): onLongPress: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=179.22656, y[0]=41.0625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34538199, downTime=34538199, deviceId=1, source=0x1002 }
09-04 14:22:13.230: I/GestureListener(17689): gestureResult = false
投掷结果:
09-04 14:24:09.650: D/GestureListener(17689): onDown: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=332.15625, y[0]=2.625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34655337, downTime=34655337, deviceId=1, source=0x1002 }
09-04 14:24:09.650: I/GestureListener(17689): gestureResult = true
09-04 14:24:09.690: D/GestureListener(17689): onScroll: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=332.15625, y[0]=2.625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34655337, downTime=34655337, deviceId=1, source=0x1002 }MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=331.9604, y[0]=-72.36835, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=2, eventTime=34655368, downTime=34655337, deviceId=1, source=0x1002 }
09-04 14:24:09.690: I/GestureListener(17689): gestureResult = true
09-04 14:24:09.710: D/GestureListener(17689): onScroll: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=332.15625, y[0]=2.625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34655337, downTime=34655337, deviceId=1, source=0x1002 }MotionEvent { action=ACTION_MOVE, id[0]=0, x[0]=337.91425, y[0]=-124.40411, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=2, eventTime=34655385, downTime=34655337, deviceId=1, source=0x1002 }
09-04 14:24:09.710: I/GestureListener(17689): gestureResult = true
09-04 14:24:09.730: D/GestureListener(17689): onFling: MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=332.15625, y[0]=2.625, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34655337, downTime=34655337, deviceId=1, source=0x1002 }MotionEvent { action=ACTION_UP, id[0]=0, x[0]=337.91425, y[0]=-124.40411, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=34655414, downTime=34655337, deviceId=1, source=0x1002 }
09-04 14:24:09.730: I/GestureListener(17689): gestureResult = true