我正在關注Detecting common gestures指南。我已經掛android-support-v4.jar
庫得到GestureDetectorCompat
,和我的代碼看起來完全一樣,在指南中,除了我檢測我的自定義視圖的手勢,而不是活動:GestureDetector.SimpleOnGestureListener和GestureDetectorCompat不起作用。我的代碼有什麼問題?
public class MyGlView extends GLSurfaceView {
private GestureDetectorCompat m_gestureDetector = null;
public MyGlView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MyGlView(Context context) {
super(context);
init(context);
}
private void init(Context context) {
if (m_gestureDetector == null)
m_gestureDetector = new GestureDetectorCompat(context, new MyGestureListener());
setEGLContextClientVersion(2);
setRenderer(new DrawSurfRenderer());
setRenderMode(RENDERMODE_CONTINUOUSLY);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
m_gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
public class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
Log.e("", "OnScroll: deltaX=" + String.valueOf(e2.getX() - e1.getX()) + ", deltaY=" + String.valueOf(e2.getY() - e1.getY()));
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e)
{
Log.e("", "onSingleTapUp: X=" + String.valueOf(e.getX()) + ", Y=" + String.valueOf(e.getY()));
return true;
}
@Override
public void onLongPress(MotionEvent e)
{
Log.e("", "onLongPress: X=" + String.valueOf(e.getX()) + ", Y=" + String.valueOf(e.getY()));
}
}
不管我做什麼用觸摸屏,我只得到onLongPress
。事實上,當我快速點擊(快速觸摸並釋放屏幕)時,我從屏幕上移開手指(懷疑是長時間點擊檢測延遲)後,仍然略微得到onLongPress
。
有什麼收穫?
你嘗試改變'返回super.onTouchEvent(event);'返回false;'?並嘗試你的簡單視圖(如WebView的例子)的行動。 – 2013-11-01 14:48:10
@ R.id.pandacoder:'return false;'不會改變任何東西。 –
經過一番調查,我認爲你可以嘗試添加'onDown'方法,它返回true到你的'onTouchEvent'方法。 – 2013-11-01 15:09:38