How to add swipe left of right in xamarin android ?

I am searching from a long time but could not find the solution. I have build webview which shows different pages on left or right buttons, but now I want to add swipe functionality for next or previous pages.

Answers

  • D3nnisD3nnis ✭✭✭ USUniversity ✭✭✭

    That's a Xamarin Forms example. He was asking about Android.

    What do you want to achieve? Do you want to go back when you swipe left and go to the next page if you swipe right? Or do you want to open a different tab in your webview when you swipe?

  • UmairShaukatUmairShaukat PKMember

    @Dennis, Yes I want to go back when I swipe left and go to next page when I swipe right.

  • D3nnisD3nnis ✭✭✭ USUniversity ✭✭✭
    edited January 2017

    This should do what you want. You could change it a bit to make it more or less sensitive or add more actions to it.

            float startX = 0;
            float webViewWidth = 0;
    
            webView.Touch += (sender, e) => {
                if (e.Event.Action == Android.Views.MotionEventActions.Down) {
                    webViewWidth = webView.Width;
                    startX = e.Event.GetX ();
                }
                if (e.Event.Action == Android.Views.MotionEventActions.Up) {
                    float movement = e.Event.GetX () - startX;
                    float offset = webViewWidth / 2;
    
                    if (Math.Abs (movement) > offset) {
                        if (movement < 0) {
                            System.Console.WriteLine ("Left swipe");
                            webView.GoBack ();
                        } else { 
                            System.Console.WriteLine ("Right swipe");
                            webView.GoForward ();
                        }
                    }
                }
                e.Handled = false;
            };
    
  • LearnEverythingLearnEverything ✭✭✭ USMember ✭✭✭

    for activity this does not work blogs.microsoft.co.il/sasha/2014/03/20/android-gesture-recognition-animation-xamarin/

    public class MainActivity : Activity
    {
        private GestureDetector _detector;
    
        private class GestureListener : GestureDetector.SimpleOnGestureListener
        {
            public override bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
            {
                // TODO
                return true;
            }
        }
    
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
    
            var listener = new GestureListener();
            _detector = new GestureDetector(this, listener);
        }
    
        protected override bool OnTouchEvent(MotionEvent e)
        {
            _detector.OnTouchEvent(e);
            return base.OnTouchEvent(e);
        }
    }
    
Sign In or Register to comment.