Распознавание событий касания на Xamarin Android и «событие отбрасывания из-за отсутствия фокуса окна» - PullRequest
0 голосов
/ 17 февраля 2020

Я пытаюсь обработать пролистывание влево, пролистывание вправо и двойное касание в приложении SmartWatch Xamarin Android.

Я использовал этот пост в качестве руководства, однако я не могу позвонить OnSingle, OnLongPress, OnSwipeRight, OnSwipeLeft ...

Я получаю следующий вывод:

02-17 10:29:36.893 W/ViewRootImpl[MainActivity]( 4450): Dropping event due to no window focus: MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=46.893387, y[0]=209.68695, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=2, eventTime=2834886, downTime=2834684, deviceId=0, source=0x1002 }

Что я отсутствует или делает неправильно?

Код:

[Activity(Label = "@string/app_name", MainLauncher = true)]
public class MainActivity : WearableActivity, GestureDetector.IOnGestureListener
{
    private GestureDetector gestureDetector;
    const int SWIPE_DISTANCE_THRESHOLD = 100;
    const int SWIPE_VELOCITY_THRESHOLD = 100;

    /// <summary>
    /// The on create.
    /// </summary>
    /// <param name="bundle">
    /// The bundle.
    /// </param>
    protected override async void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        this.SetContentView(Resource.Layout.activity_main);
        this.gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener());

        TextView text = this.FindViewById<TextView>(Resource.Id.text);
        this.SetAmbientEnabled();

        while (true)
        {
            //...code...

            await Task.Delay(5000);

            //...code...
        }

    }

    public bool OnDown(MotionEvent e)
    {
        return true;
    }

    public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        float distanceX = e2.GetX() - e1.GetX();
        float distanceY = e2.GetY() - e1.GetY();
        if (Math.Abs(distanceX) > Math.Abs(distanceY) && Math.Abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.Abs(velocityX) > SWIPE_VELOCITY_THRESHOLD)
        {
            if (distanceX > 0)
            {
                this.OnSwipeRight();
            }
            else
            {
                this.OnSwipeLeft();
            }

            return true;
        }
        return false;
    }

    void OnSwipeLeft()
    {
        TextView serviceScoreLabel = this.FindViewById<TextView>(Resource.Id.serviceScore);
        serviceScoreLabel.Text = "LLLLL";
    }

    void OnSwipeRight()
    {
        TextView serviceScoreLabel = this.FindViewById<TextView>(Resource.Id.serviceScore);
        serviceScoreLabel.Text = "RRRRR";
    }

    public void OnLongPress(MotionEvent e)
    {
        TextView serviceScoreLabel = this.FindViewById<TextView>(Resource.Id.serviceScore);
        serviceScoreLabel.Text = "long press";
    }

    public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
    {
        return false;
    }

    public void OnShowPress(MotionEvent e)
    {
        TextView serviceScoreLabel = this.FindViewById<TextView>(Resource.Id.serviceScore);
        serviceScoreLabel.Text = "show press";
    }

    public bool OnSingleTapUp(MotionEvent e)
    {
        TextView serviceScoreLabel = this.FindViewById<TextView>(Resource.Id.serviceScore);
        serviceScoreLabel.Text = "single tap";
        return false;
    }

    public bool OnTouch(View v, MotionEvent e)
    {
        return gestureDetector.OnTouchEvent(e);
    }
}

1 Ответ

1 голос
/ 18 февраля 2020

Вы должны переопределить OnTouchEvcent метод, вы пропустили его?

[Activity(Label = "SwipActivity",MainLauncher =true)]
public class SwipActivity : Activity, GestureDetector.IOnGestureListener
{
    AlertDialog _dialog;
    GestureDetector gestureDetector;
    const int SWIPE_DISTANCE_THRESHOLD = 100;
    const int SWIPE_VELOCITY_THRESHOLD = 100;
    protected override void OnCreate(Bundle savedInstanceState)
    {

        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.swip);


        gestureDetector = new GestureDetector(this);
    }

public bool OnDown(MotionEvent e)
    {
        return true;
    }

    public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        float distanceX = e2.GetX() - e1.GetX();
        float distanceY = e2.GetY() - e1.GetY();
        if (Math.Abs(distanceX) > Math.Abs(distanceY) && Math.Abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.Abs(velocityX) > SWIPE_VELOCITY_THRESHOLD)
        {
            if (distanceX > 0)
                OnSwipeRight();
            else
                OnSwipeLeft();
            return true;
        }
        return false;
    }

    void OnSwipeLeft()
    {

        Toast.MakeText(this, "Swip left", ToastLength.Short).Show();
    }

    void OnSwipeRight()
    {

        Toast.MakeText(this, "Swip right", ToastLength.Short).Show();
    }

    public void OnLongPress(MotionEvent e)
    {

        Toast.MakeText(this, "OnLongPress", ToastLength.Short).Show();
    }

   public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
    {
        return false;
    }

    public void OnShowPress(MotionEvent e)
    {
    }

    public bool OnSingleTapUp(MotionEvent e)
    {
        return false;
    }

    public bool OnTouch(View v, MotionEvent e)
    {
        return gestureDetector.OnTouchEvent(e);
    }

    public override bool OnTouchEvent(MotionEvent e)
    {
        gestureDetector.OnTouchEvent(e);
        return false;
    }

}
...