Detection of swiping on a normal Android Activity

二次信任 提交于 2020-03-01 21:57:40

问题


Is it possible, to detect a swiping to the left/right of the user in a normal Android activity? Or do I have to use a SwipeView?


回答1:


If you are looking for a Xamarin/C# example

Implement GestureDetector.IOnGestureListener on your Activity (or view) and calculate the direction of the touch in the OnFling method:

[Activity(Label = "Swiper", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity, GestureDetector.IOnGestureListener
{
    GestureDetector gestureDetector;
    const int SWIPE_DISTANCE_THRESHOLD = 100;
    const int SWIPE_VELOCITY_THRESHOLD = 100;

    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()
    {
        Button button = FindViewById<Button>(Resource.Id.myButton);
        button.Text = "Swiped Left";
    }

    void OnSwipeRight()
    {
        Button button = FindViewById<Button>(Resource.Id.myButton);
        button.Text = "Swiped Right";
    }

    public void OnLongPress(MotionEvent e)
    {
    }

    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;
    }
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Main);

        gestureDetector = new GestureDetector(this);
    }
}



回答2:


According with https://developer.android.com/training/gestures/detector.html

You can implement GestureDetector.OnGestureListener in your activity class and override onFling or onScroll

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString());
    return true;
}


@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
        float velocityX, float velocityY) {
    Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString());
    return true;
}


来源:https://stackoverflow.com/questions/38722272/detection-of-swiping-on-a-normal-android-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!