OnLongPress无法处理我们的触摸事件

时间:2018-09-30 10:00:11

标签: android xamarin.android ontouchevent

我正在开发一款游戏,我想在用户单次触摸屏幕时执行某些操作,而在用户长按屏幕时执行一些其他操作。为此,我创建了一个Gesture Detector类,并向其中添加事件。

表面视图类

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); 
    }

手势监听器类

  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事件返回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与其他事件一起工作,因为我需要所有事件一起工作。

1 个答案:

答案 0 :(得分:0)

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

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

您可以找到说明here

相关问题