繁体   English   中英

如何在 Xamarin.Android 中实现向左/向右滑动?

[英]How can I implement a swipe left/right in Xamarin.Android?

我是 Xamarin Android 的新手。 我想在线性布局中实现向左/向右滑动。

例如:

  • 向左滑动时 --> 方法“Open()”
  • 向右滑动时 --> 方法“Close()”

我的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:src="@drawable/reddoor"
    android:layout_width="match_parent"
    android:layout_height="230dp"
    android:id="@+id/imgDoor"
    android:layout_marginTop="50px"
    android:layout_alignParentBottom="false"
    android:layout_alignParentEnd="false"
    android:layout_alignParentLeft="true"
    android:foregroundGravity="fill"
    android:layout_marginRight="0.0dp" />

<Button
    android:text="Open"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/btnOpenDoor" />
<Button
    android:text="Close"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/btnCloseDoor" />

我该如何实施? 我进行了搜索,但我刚刚找到了 Xamarin.Forms 的一些东西,但我需要 Xamarin.Android。 有人可以帮忙吗?

根据你的描述,你想在xamarin.android中实现swipe功能,我做了一个示例,你可以看看:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px"
android:gravity="center">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="swap"
    android:id="@+id/button1"
    android:layout_gravity="center" />
</LinearLayout>

 private Button btn1;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        btn1 = FindViewById<Button>(Resource.Id.button1);
        btn1.SetOnTouchListener(new OnSwipeTouchListener(this));

    }

  public class OnSwipeTouchListener : Java.Lang.Object , View.IOnTouchListener
{
    private  GestureDetector gestureDetector;
   public OnSwipeTouchListener(Context ctx)
    {
        gestureDetector = new GestureDetector(ctx, new GestureListener());
    }

    public bool OnTouch(View v, MotionEvent e)
    {
        return gestureDetector.OnTouchEvent(e);
    }
}
public class GestureListener : GestureDetector.SimpleOnGestureListener
{
    private static  int SWIPE_THRESHOLD = 100;
    private static  int SWIPE_VELOCITY_THRESHOLD = 100;
    public override bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        // TODO
        bool result = false;
        try
        {
            float diffY = e2.GetY() - e1.GetY();
            float diffX = e2.GetX() - e1.GetX();
            if (Math.Abs(diffX) > Math.Abs(diffY))
            {
                if (Math.Abs(diffX) > SWIPE_THRESHOLD && Math.Abs(velocityX) > SWIPE_VELOCITY_THRESHOLD)
                {
                    if (diffX > 0)
                    {
                        onSwipeRight();
                    }
                    else
                    {
                        onSwipeLeft();
                    }
                    result = true;
                }
            }
            else if (Math.Abs(diffY) > SWIPE_THRESHOLD && Math.Abs(velocityY) > SWIPE_VELOCITY_THRESHOLD)
            {
                if (diffY > 0)
                {
                    onSwipeBottom();
                }
                else
                {
                    onSwipeTop();
                }
                result = true;
            }
        }
        catch (Exception exception)
        {
            _ = exception.StackTrace;
        }
        return result;

    }
    public void onSwipeRight()
    {
        Toast.MakeText(Application.Context, "right", ToastLength.Short).Show();
    }
    public void onSwipeLeft()
    {
        Toast.MakeText(Application.Context, "left", ToastLength.Short).Show();
    }
    public void onSwipeTop()
    {
        Toast.MakeText(Application.Context, "top", ToastLength.Short).Show();
    }
    public void onSwipeBottom()
    {
        Toast.MakeText(Application.Context, "up", ToastLength.Short).Show();
    }


}

截图:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM