Custom ScrollView renderer does not work scroll

ViktorBylbasViktorBylbas UAMember ✭✭
edited January 30 in Xamarin.Android

I create custom ScrollView.
This is done so that the swipes and scrolls work.
I need to make a vertical scroll.
Help me please

`
public class GestureScrollView : ScrollView
{
public event EventHandler SwipeDown;
public event EventHandler SwipeTop;
public event EventHandler SwipeLeft;
public event EventHandler SwipeRight;

    public void OnSwipeDown() =>
        SwipeDown?.Invoke(this, null);

    public void OnSwipeTop() =>
        SwipeTop?.Invoke(this, null);

    public void OnSwipeLeft()
    {
        SwipeLeft?.Invoke(this, null);
    }

    public void OnSwipeRight()
    {
        SwipeRight?.Invoke(this, null);
    }
}`

Android

`[assembly: ExportRenderer(typeof(GestureScrollView), typeof(Swipe.Droid.Platform.Renderers.GestureScrollViewRenderer))]
namespace Swipe.Droid.Platform.Renderers
{
public class GestureScrollViewRenderer : ScrollViewRenderer
{
readonly CustomGestureListener _listener;
readonly GestureDetector _detector;

    public GestureScrollViewRenderer(Context context) : base(context)
    {
        _listener = new CustomGestureListener();
        _detector = new GestureDetector(context, _listener);
    }

    public override bool DispatchTouchEvent(MotionEvent e)
    {
        if (_detector != null)
        {
            _detector.OnTouchEvent(e);
            base.DispatchTouchEvent(e);
            return true;
        }
        return base.DispatchTouchEvent(e);
    }

    public override bool OnTouchEvent(MotionEvent ev)
    {
        base.OnTouchEvent(ev);

        if (_detector != null)
            return _detector.OnTouchEvent(ev);

        return false;
    }

    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);

        if (e.NewElement == null)
        {
            this.GenericMotion -= HandleGenericMotion;
            this.Touch -= HandleTouch;
            _listener.OnSwipeLeft -= HandleOnSwipeLeft;
            _listener.OnSwipeRight -= HandleOnSwipeRight;
            _listener.OnSwipeTop -= HandleOnSwipeTop;
            _listener.OnSwipeDown -= HandleOnSwipeDown;
        }

        if (e.OldElement == null)
        {
            this.GenericMotion += HandleGenericMotion;
            this.Touch += HandleTouch;
            _listener.OnSwipeLeft += HandleOnSwipeLeft;
            _listener.OnSwipeRight += HandleOnSwipeRight;
            _listener.OnSwipeTop += HandleOnSwipeTop;
            _listener.OnSwipeDown += HandleOnSwipeDown;
        }
    }

    void HandleTouch(object sender, TouchEventArgs e)
    {
        _detector.OnTouchEvent(e.Event);
    }

    void HandleGenericMotion(object sender, GenericMotionEventArgs e)
    {
        _detector.OnTouchEvent(e.Event);
    }

    void HandleOnSwipeLeft(object sender, EventArgs e)
    {
        var _gi = (GestureScrollView)this.Element;
        _gi.OnSwipeLeft();
    }

    void HandleOnSwipeRight(object sender, EventArgs e)
    {
        var _gi = (GestureScrollView)this.Element;
        _gi.OnSwipeRight();
    }

    void HandleOnSwipeTop(object sender, EventArgs e)
    {
        var _gi = (GestureScrollView)this.Element;
        _gi.OnSwipeTop();
    }

    void HandleOnSwipeDown(object sender, EventArgs e)
    {
        var _gi = (GestureScrollView)Element;
        _gi.OnSwipeDown();
    }
}

public class CustomGestureListener : GestureDetector.SimpleOnGestureListener
{
    private static int SWIPE_THRESHOLD = 100;
    static readonly int SWIPE_VELOCITY_THRESHOLD = 100;

    MotionEvent mLastOnDownEvent;

    public event EventHandler OnSwipeDown;
    public event EventHandler OnSwipeTop;
    public event EventHandler OnSwipeLeft;
    public event EventHandler OnSwipeRight;

    public override bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
    {
        return base.OnScroll(e1, e2, distanceX, distanceY);
    }

    public override bool OnDown(MotionEvent e)
    {
        mLastOnDownEvent = e;

        return true;
    }

    public override bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        Console.WriteLine("OnFling");

        if (e1 == null)
            e1 = mLastOnDownEvent;

        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?.Invoke(this, null);
                else
                    OnSwipeLeft?.Invoke(this, null);
            }
        }
        else if (Math.Abs(diffY) > SWIPE_THRESHOLD && Math.Abs(velocityY) > SWIPE_VELOCITY_THRESHOLD)
        {
            if (diffY > 0)
                OnSwipeDown?.Invoke(this, null);
            else
                OnSwipeTop?.Invoke(this, null);
        }

        return base.OnFling(e1, e2, velocityX, velocityY);
    }
}

}`

Xaml:

<ctrl:GestureScrollView x:Name="gi"> ... </ctrl:GestureScrollView>

C#:

`gi.SwipeDown += (s, e) =>
{
DisplayAlert("Gesture Info", "Swipe Down Detected", "OK");
// scroll down
};

        gi.SwipeTop += (s, e) =>
        {
            DisplayAlert("Gesture Info", "Swipe Top Detected", "OK");
            // scroll top
        };

        gi.SwipeLeft += (s, e) =>
        {
            DisplayAlert("Gesture Info", "Swipe Left Detected", "OK");
            ((NewsFeedDetailPageModel)BindingContext).OpenSourceCommand.Execute(null);
        };

        gi.SwipeRight += (s, e) =>
        {
            DisplayAlert("Gesture Info", "Swipe Right Detected", "OK");
            ((NewsFeedDetailPageModel)BindingContext).BackCommand.Execute(null);
        };`

Best Answer

Answers

Sign In or Register to comment.