Swiping Fragments with button click

AyhamNajemAyhamNajem USMember
edited June 2016 in Xamarin.Forms

Hello everyone,

I have 5 fragments these I want to swipe back and forward with buttons, I actually made a class to stop swiping with just touching, but I couldn't reach a good sulotion to make a swipe between two fragments with a button, so maybe can someone help me please.

This is the fragment adapter in the main activity:

public class AFragmentAdapter : FragmentPagerAdapter
    {

        public AFragmentAdapter(Android.Support.V4.App.FragmentManager fm) : base(fm)
        {

        }

        public override int Count
        {
            get
            {
                return 5;
            }
        }

        public override Android.Support.V4.App.Fragment GetItem(int position)
        {
            switch (position)
            {            
                case 0: return new AFragment();
                case 1: return new BFragment();
                case 2: return new CFragment();
                case 3: return new DFragment();
                case 4: return new EFragment();
                default: return new AFragment();
            }

        }

    }

And this is my fragment activity so far:

public class a : FragmentActivity
    {

        private Button nxt1;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.a);

            nxt1 = FindViewById<Button>(Resource.Id.but1);
            nxt1.Click += Nxt1_Click;
        }

        private void Nxt1_Click(object sender, EventArgs e)
        {

        }

    }

And this is my Class to stop swiping with touches:

 public class NonSwipeableViewPager : ViewPager
    {

        private new bool Enabled;

        public NonSwipeableViewPager(IntPtr handle, JniHandleOwnership transfer)
            : base(handle, transfer)
        {

        }

        public NonSwipeableViewPager(Context context, IAttributeSet attrs) : base(context, attrs)
        {
            this.Enabled = true;
        }


        public override bool OnTouchEvent(MotionEvent e)
        {
            if (this.Enabled)
            {
                return base.OnTouchEvent(e);
            }
            return false;
        }

        public override bool OnInterceptTouchEvent(MotionEvent ev)
        {
            if (this.Enabled)
            {
                return base.OnInterceptTouchEvent(ev);
            }
            return false;
        }

        public void SetPagingEnabled(bool enabled)
        {
            this.Enabled = enabled;
        }
    }

Thanks :smile:

Answers

  • HunumanHunuman GBMember ✭✭✭✭
    edited June 2016

    @AyhamNajem

    Looks like a Android specific question.....

    If so, I suggest that you post this question in the Android forum, this forum is Xamarin.Forms specific.
    If not, my bad.

    Hope this helps,

    Tim

Sign In or Register to comment.