OnLongPress 不处理我们的触摸事件

OnLongPress is not working with our touch events

我正在开发一款游戏,我想在用户单击屏幕时执行某些操作,而在用户长按屏幕时执行其他操作。为此,我创建了一个手势检测器 class 并向其添加事件。

表面视图Class

public MySurfaceView(Context context, IAttributeSet attrs):base(context, attrs)
    {
        this.context=context;
        SetWillNotDraw(false);
        gestureDetector = new GestureDetector(context, new GestureListener());

    }

 public override bool OnTouchEvent(MotionEvent e)
    {
        Log.Debug(Tag, "Inside" + System.Reflection.MethodBase.GetCurrentMethod().Name + "Method");
        return gestureDetector.OnTouchEvent(e); 
    }

手势侦听器Class

  private class GestureListener : GestureDetector.SimpleOnGestureListener
    {
        public override bool OnDown(MotionEvent e)
        {
            Log.Debug("Tag", "Inside Gesture OnDown Event");
            // don't return false here or else none of the other 
            // gestures will work
            return true;

        }

        public override bool OnSingleTapConfirmed(MotionEvent e)
        {
            Log.Debug("Tag", "Inside Gesture OnSingleTapConfirmed Event");

            return true;
        }

        public override bool OnDoubleTap(MotionEvent e)
        {
            Log.Debug("Tag", "Inside Gesture OnDoubleTap Event");
            return true;
        }

        public override void OnLongPress(MotionEvent e)
        {
            Log.Debug("Tag", "Inside Long Press Event");
        }




    }

除 OnLongPress 之外的所有事件均使用上述代码。在浏览了这个 question 的评论之后。对于 OnDown 事件,我必须 return false。根据评论更新我的代码库后,我的 OnLongPress 事件开始工作,但现在只有 OnLongPress 事件在工作。

   public override bool OnDown(MotionEvent e)
        {
            Log.Debug("Tag", "Inside Gesture OnDown Event");
            // don't return false here or else none of the other 
            // gestures will work
            return false;

        }

有什么方法可以让 OnLongPress 与其他事件一起工作,因为我需要所有事件一起工作。

将您的 OnTouchEvent 更改为以下内容:

 public override bool OnTouchEvent(MotionEvent e){
     Log.Debug(Tag, "Inside" + System.Reflection.MethodBase.GetCurrentMethod().Name + "Method");
     gestureDetector.OnTouchEvent(e); 
     return true;
}

你可以找到解释here