在你的GestureDetector中,你可以直接調用callOnClick()。 請注意View.callOnClick API需要API級別15. 只需嘗試一下。
// Create a Gesturedetector
GestureDetector mGestureDetector = new GestureDetector(context, new MyGestureDetector());
// Add a OnTouchListener into view
m_myViewer.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
return mGestureDetector.onTouchEvent(event);
}
});
private class MyGestureDetector extends GestureDetector.SimpleOnGestureListener
{
public boolean onSingleTapUp(MotionEvent e) {
// ---Call it directly---
callOnClick();
return false;
}
public void onLongPress(MotionEvent e) {
}
public boolean onDoubleTap(MotionEvent e) {
return false;
}
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}
public void onShowPress(MotionEvent e) {
LogUtil.d(TAG, "onShowPress");
}
public boolean onDown(MotionEvent e) {
// Must return true to get matching events for this down event.
return true;
}
public boolean onScroll(MotionEvent e1, MotionEvent e2, final float distanceX, float distanceY) {
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
// do something
return super.onFling(e1, e2, velocityX, velocityY);
}
}
爲什麼你需要onTouch和onClick?當然只要onTouch就足夠了。 – JDx
[GestureDetector](http://developer.android.com/reference/android/view/GestureDetector.html)如何? – keyser
@JDx我該如何識別來自'onTouchListener'的點擊?點擊是至少兩個觸摸事件的序列,其中「ACTION_DOWN」事件在與後面的「ACTION_UP」事件相同的位置(或至少在相同的視圖上)觸發。當然,我們可以實現這一點,如果沒有其他解決方案,我會這樣做。但是我的問題的確是,是否有針對這種情況的內置解決方案? – theV0ID