Articles in this section
Category / Section

How to detect scrolling direction in Xamarin Forms ListView (SfListView)?

2 mins read

You can find the scrolling direction in Xamarin.Forms SfListView by using the Scrolled event of the ScollView.

XAML

In the SfListView.HeaderTemplate, bind the ViewModel property to show the scroll direction.

<ContentPage xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms">
    <syncfusion:SfListView x:Name="listView" 
                           ItemsSource="{Binding ContactsInfo}"
                           IsStickyHeader="True"
                           ItemSize="70">
        <syncfusion:SfListView.Behaviors>
            <local:Behavior/>
        </syncfusion:SfListView.Behaviors>
        <syncfusion:SfListView.HeaderTemplate>
            <DataTemplate>
                <Label Text="{Binding ScrollDirection}" />
            </DataTemplate>
        </syncfusion:SfListView.HeaderTemplate>
        <syncfusion:SfListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Image Source="{Binding ContactImage}" 
                           Aspect="AspectFill"/>
                    <Label LineBreakMode="NoWrap"
                           Text="{Binding ContactName}"/>
                    <Label LineBreakMode="NoWrap"
                           Text="{Binding ContactNumber}"/>
                    <Label LineBreakMode="NoWrap"
                           Text="{Binding ContactType}"/>
                </Grid>
            </DataTemplate>
        </syncfusion:SfListView.ItemTemplate>
    </syncfusion:SfListView>
</ContentPage>
 

 

C#

You can get the ExtendedScrollView using the ListView.GetScrollView helper method and update the ScrollDirection value based on the previous offset.

public class Behavior : Behavior <SfListView>
{
    ExtendedScrollView scrollview;
    double previousOffset;
    public SfListView listview { get; private set; }
 
    protected override void OnAttachedTo(SfListView bindable)
    {
        base.OnAttachedTo(bindable);
        listview = bindable as SfListView;
        scrollview = listview.GetScrollView();
        scrollview.Scrolled += Scrollview_Scrolled;
    }
 
    private void Scrollview_Scrolled(object sender, ScrolledEventArgs e)
    {
        if (e.ScrollY == 0)
            return;
 
        if (previousOffset >= e.ScrollY)
        {
            // Up direction  
            viewModel.ScrollDirection = "Up Direction";
        }
        else
        {
            //Down direction 
            viewModel.ScrollDirection = "Down Direction";
        }
 
        previousOffset = e.ScrollY;
    }
}

View sample in GitHub

Scrolling direction in ListView

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied