Search code examples
c#androidiosxamarin.formsscrollview

Xamarin Forms - attaching scrollview.Scrolled to a function


When I attach a scrollview to a function like this

textScroll.Scrolled += (sender, e) => { onScrolled(); };

Each time I scroll up or down, OnScrolled() is called multiple times. I know I can get the size of the content and compare it to the ScrollY value, obviously the ScrollY value changes each time, but as far as I can see I won't know when the last call happens (per user scroll).

I only want to call this once per scroll, failing that call it each time as is happening now, but only act when I know I'm on the last call.

Is this possible?

thanks


Solution

  • It is possible, but with a custom renderer for each platform.

    On iOS: you will want to implement delegates for DecelerationEnded and WillEndDragging. The reason for also implementing DecelerationEnded is to allow for a fling by the user and waiting for the velocity to come to 0.

    On Android it is a bit more complicated. Here is a native Android SO post I followed and translated into c# in a renderer. Works pretty well for me. Android: Detect when ScrollView stops scrolling

    After having both implemented, you can call to your Xamarin.Forms view in order to notify that the view has Stopped scrolling (i.e. final call)