简体   繁体   中英

How to disable left swipe and enable right swipe in xamarin android

I have a viewpager with 3 fragments. In the second fragment i have a button. Currently the swiping is happening both left and right for all the 3 fragments. Now in the second fragment only after clicking on the button user should be able to go to 3rd screen. Otherwise from second screen to third screen swipe should be blocked. When user comes to third screen then he can go back to previous screens. I have written the customviewpager code but its not working for blocking a specific swipe. Please help me with this issue.

CustomViewPager.cs

namespace swiping
{
    public class CustomViewPager : ViewPager
    {
        private float initialXValue;
        private SwipeDirection direction;
        private int position = 0;
        CustomViewPager myadapter;

        bool isrightswipe, isleftswipe;

        public bool ShouldAllowSwipe()
        {

            if (this.CurrentItem == 1 && isleftswipe == true)
            {

                return false;
            }

            return true;

        }



        public CustomViewPager(Context context, IAttributeSet attrs) : base(context, attrs)
        {

            //this.direction = SwipeDirection.all;

        }

        public override bool OnTouchEvent(MotionEvent e)
        {
            //base.OnTouchEvent(e);

            //if (this.IsSwipeAllowed(e))
            //{
            //  return base.OnTouchEvent(e);
            //}

            IsSwipeAllowed(e);
            if (ShouldAllowSwipe())
            {
                base.OnTouchEvent(e);
                return true;
            }





            return false;
        }





        public override bool OnInterceptTouchEvent(MotionEvent e)
        {

            if (this.IsSwipeAllowed(e))
            {
                return base.OnInterceptTouchEvent(e);
            }

            return false;
        }

        private bool IsSwipeAllowed(MotionEvent e)
        {
            //if (this.direction == SwipeDirection.all) return true;

            //if (direction == SwipeDirection.none)//disable any swipe
            //return false;

            isrightswipe = false;
            isleftswipe = false;

            if (e.Action == MotionEventActions.Down)
            {
                initialXValue = e.GetX();

            }

            if (e.Action == MotionEventActions.Move)
            {
                try
                {
                    float diffX = e.GetX() - initialXValue;
                    if (diffX > 0 && direction == SwipeDirection.right)
                    {
                        // swipe from left to right detected
                        //prev
                        isleftswipe = false;
                        isrightswipe = true;
                        return false;
                    }
                    else if (diffX < 0 && direction == SwipeDirection.left)
                    {
                        // swipe from right to left detected
                        //next
                        isleftswipe = true;
                        isrightswipe = false;
                        return false;
                    }
                }
                catch (Exception ex)
                {

                }
            }

            return true;
        }

        public void setAllowedSwipeDirection(SwipeDirection direction)
        {
            this.direction = direction;
        }

    }


    public enum SwipeDirection
    {
        all, left, right, none
    }
} 

I refactored your code and completed the solution.

using System;
using Android.Content;
using Android.Runtime;
using Android.Support.V4.View;
using Android.Util;
using Android.Views;

namespace Android.Base
{
    [Register("ViewPagerWithCustomSwipe")]
    public class ViewPagerWithCustomSwipe : ViewPager
    {
        private float InitialX;
        private SwipeDirection DisabledSwipeDirection;

        public ViewPagerWithCustomSwipe(Context context, IAttributeSet attrs) : base(context, attrs)
        {
        }

        protected ViewPagerWithCustomSwipe(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
        {
        }

        public ViewPagerWithCustomSwipe(Context context) : base(context)
        {
        }

        public override bool OnTouchEvent(MotionEvent e)
        {
            if (IsSwipeAllowed(e))
                return base.OnTouchEvent(e);

            return false;
        }

        public override bool OnInterceptTouchEvent(MotionEvent e)
        {
            if (IsSwipeAllowed(e))
                return base.OnInterceptTouchEvent(e);

            return false;
        }

        private bool IsSwipeAllowed(MotionEvent e)
        {
            if (DisabledSwipeDirection == SwipeDirection.All)
                return false;

            if (DisabledSwipeDirection == SwipeDirection.None)
                return true;

            if (e.Action == MotionEventActions.Down)
                InitialX = e.GetX();

            if (e.Action == MotionEventActions.Move)
            {
                float diffX = e.GetX() - InitialX;
                if (diffX > 0 && DisabledSwipeDirection == SwipeDirection.Right)
                    return false;

                else if (diffX < 0 && DisabledSwipeDirection == SwipeDirection.Left)
                    return false;
            }

            return true;
        }

        public void DisableSwipe(SwipeDirection direction)
        {
            DisabledSwipeDirection = direction;
        }
    }

    public enum SwipeDirection
    {
        All = 0,
        Left = 1,
        Right = 2,
        None = 3
    }
}

You need to use control and add OnPageChangeListener to it. At custom page listener you can implement your own logic with disabling the swipe.

How to use it you can see on my github: https://gist.github.com/IlyaLavrov97/4bf2fb11ea195a0bbbaa2276a1a6c586

Happy codding! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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