I have a scrollview which, as soon as the user starts to scroll up, I run some code (OnSwipeUp()). I detect this using
textScroll.Scrolled += (sender, e) => { onScrolled(); };
private void onScrolled()
{
if(textScroll.ScrollY > 0)
{
OnSwipeUp(null, null);
}
}
I also need to detect when the user start scrolling down, so I was thinking of getting the max Y position of the scrollview then say something like
if(textScroll.ScrollY < maxY)
Am I able to do this?
Try this:
private double previousScrollPosition = 0;
void Handle_Scrolled(object sender, Xamarin.Forms.ScrolledEventArgs e)
{
if (previousScrollPosition < e.ScrollY)
{
//scrolled down
previousScrollPosition = e.ScrollY;
}
else
{
//scrolled up
if (Convert.ToInt16(e.ScrollY) == 0)
previousScrollPosition = 0;
}
}
You do not need to get the max Y position because before scrolling down, the Y position is always 0. So you just have to get the direction of scrolling. I wrote a demo to prove this:
MainPage.xaml.cs
private double previousScrollPosition = 0;
public MainPage()
{
InitializeComponent();
}
void Handle_Scrolled(object sender, Xamarin.Forms.ScrolledEventArgs e)
{
if (previousScrollPosition < e.ScrollY)
{
//scrolled down
Console.WriteLine("!!!!!!!!!scrolled down ScrollY=>" + textScroll.ScrollY);
}
else
{
//scrolled up
Console.WriteLine("!!!!!!!!!scrolled up ScrollY=>" + textScroll.ScrollY);
}
previousScrollPosition = e.ScrollY;
}
MainPage.xaml
<ContentPage.Content>
<StackLayout>
<BoxView BackgroundColor="Red" HeightRequest="200" WidthRequest="150" />
<ScrollView x:Name="textScroll" Scrolled="Handle_Scrolled">
<StackLayout>
<Entry Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/>
<Entry Text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"/>
<Entry Text="ccccccccccccccccccccccccccccccccccccccccccccccccccc"/>
<Entry Text="ddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"/>
<Entry Text="eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"/>
<Entry Text="fffffffffffffffffffffffffffffffffffffffffff"/>
<Entry Text="ggggggggggggggggggggggggggggggggggggggggggggg"/>
<Entry Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/>
<Entry Text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"/>
<Entry Text="ccccccccccccccccccccccccccccccccccccccccccccc"/>
<Entry Text="dddddddddddddddddddddddd"/>
<Entry Text="eeeeeeeeeeeeeeeeeeeeeeeeee"/>
<Entry Text="fffffffffffffffffffffffffffffffffffffffffff"/>
<Entry Text="ggggggggggggggggggggggggggggggggggggggggggggg"/>
<Entry Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/>
<Entry Text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"/>
<Entry Text="ccccccccccccccccccccccccccccccccccccccccccccccccccc"/>
<Entry Text="ddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"/>
<Entry Text="eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"/>
<Entry Text="fffffffffffffffffffffffffffffffffffffffffff"/>
<Entry Text="ggggggggggggggggggggggggggggggggggggggggggggg"/>
<Entry Text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"/>
<Entry Text="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"/>
<Entry Text="ccccccccccccccccccccccccccccccccccccccccccccc"/>
<Entry Text="dddddddddddddddddddddddd"/>
<Entry Text="eeeeeeeeeeeeeeeeeeeeeeeeee"/>
<Entry Text="fffffffffffffffffffffffffffffffffffffffffff"/>
<Entry Text="ggggggggggggggggggggggggggggggggggggggggggggg"/>
</StackLayout>
</ScrollView>
</StackLayout>
</ContentPage.Content>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With