首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >swipe在Xamarin android中无法正常工作

swipe在Xamarin android中无法正常工作
EN

Stack Overflow用户
提问于 2021-06-02 21:41:35
回答 1查看 63关注 0票数 0

我正享受着

代码语言:javascript
复制
    <StackLayout.GestureRecognizers >
        <SwipeGestureRecognizer Direction="Left" Swiped="swiped_method"  />
        <SwipeGestureRecognizer Direction="Right" Swiped="swiped_method" />
    </StackLayout.GestureRecognizers>

但是当我刷的时候,有时候会叫刷,但有时候需要刷很多次。

EN

回答 1

Stack Overflow用户

发布于 2021-06-03 11:19:42

我确实在安卓上重现了这个问题,但它在iOS上似乎工作得很好。

这是一个针对安卓系统的潜在问题,也有人遇到过这个问题,请查看https://forums.xamarin.com/discussion/160616

我在github上提出了这个问题:https://github.com/xamarin/Xamarin.Forms/issues/14319

更新

作为一种变通方法,我们可以在android平台上使用自定义渲染器实现卷帘功能。

自定义渲染器

代码语言:javascript
复制
[assembly: ExportRenderer(typeof(StackLayout), typeof(MyRenderer))]
namespace FormsApp.Droid
{
    public class OnSwipeTouchListener : Java.Lang.Object, IOnTouchListener
    {
        private GestureDetector gestureDetector;

        public OnSwipeTouchListener(Context context )
        {
            gestureDetector = new GestureDetector(context, new GestureListener());
        }

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

        private class GestureListener : SimpleOnGestureListener {
            private static int SWIPE_THRESHOLD = 100;
            private static int SWIPE_VELOCITY_THRESHOLD = 100;

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

            public override bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
            {
                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)
                            {
                                MessagingCenter.Send<object, SwipeDirection>(this, "Hi", SwipeDirection.Right);
                            }
                            else
                            {
                                MessagingCenter.Send<object, SwipeDirection>(this, "Hi", SwipeDirection.Left);
                            }
                            result = true;
                        }
                    }
                    else if (Math.Abs(diffY) > SWIPE_THRESHOLD && Math.Abs(velocityY) > SWIPE_VELOCITY_THRESHOLD)
                    {
                        if (diffY > 0)
                        {
                            MessagingCenter.Send<object, SwipeDirection>(this, "Hi", SwipeDirection.Down);
                        }
                        else
                        {
                            MessagingCenter.Send<object, SwipeDirection>(this, "Hi", SwipeDirection.Up);
                        }
                        result = true;
                    }
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message);
                }
                return result;
            }
        }
    }



    public class MyRenderer : VisualElementRenderer<StackLayout>
    {

        Context _context;

        public MyRenderer(Context context) : base(context)
        {
            _context = context;
        }

        protected override void OnElementChanged(ElementChangedEventArgs<StackLayout> e)
        {
            base.OnElementChanged(e);


            StackLayout stackLayout = Element as StackLayout;

            if (stackLayout.GestureRecognizers.Count > 0 && stackLayout.GestureRecognizers[0] is SwipeGestureRecognizer swipe) {
                ViewGroup.SetOnTouchListener(new OnSwipeTouchListener(_context));
                MessagingCenter.Subscribe<object, SwipeDirection>(this, "Hi", (obj, direction) =>
                {
                    swipe.SendSwiped(stackLayout, direction);
                });
            }  
        }
    }
}

这样,每次都可以触发Forms项目中的Swiped事件。

请参阅Android: How to handle right to left swipe gestures

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67806310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档