How can the scrolling direction in a .NET MAUI ListView be detected?
In .NET MAUI ListView, it is possible to detect the scrolling direction. This can be achieved by using the Scrolled event of the ScrollView.
Steps to Detect Scrolling Direction
- In the SfListView.HeaderTemplate, bind the ViewModel property to display the scroll direction.
XAML
<syncfusion:sflistview x:name="listView" itemsize="60" isstickyheader="True" itemssource="{Binding ContactsInfo}">
<syncfusion:sflistview.behaviors>
<local:listviewbehavior>
</local:listviewbehavior></syncfusion:sflistview.behaviors>
<syncfusion:sflistview.headertemplate>
<datatemplate>
<label text="{Binding ScrollDirection}" fontsize="Default" fontattributes="Italic" horizontaltextalignment="Center" horizontaloptions="CenterAndExpand" verticaloptions="CenterAndExpand">
</label></datatemplate>
</syncfusion:sflistview.headertemplate>
<syncfusion:sflistview.itemtemplate>
<datatemplate>
<grid x:name="grid">
<grid.columndefinitions>
<columndefinition width="70">
<columndefinition width="*">
</columndefinition></columndefinition></grid.columndefinitions>
<img source="{Binding ContactImage}" verticaloptions="Center" horizontaloptions="Center" heightrequest="50" widthrequest="50">
<grid grid.column="1" rowspacing="1" grid.row="0" padding="10,0,0,0" rowdefinitions="*,*" verticaloptions="Center">
<label linebreakmode="NoWrap" textcolor="#474747" grid.row="0" text="{Binding ContactName}">
<label grid.row="1" grid.column="0" textcolor="#474747" linebreakmode="NoWrap" text="{Binding ContactNumber}">
</label></label></grid>
</grid>
</datatemplate>
</syncfusion:sflistview.itemtemplate>
</syncfusion:sflistview>
- In the C# code, get the ListViewScrollView using the ListView.GetScrollView helper method. Then, update the ScrollDirection value based on the previous offset.
C#
public class ListViewBehavior : Behavior<sflistview>
{
#region Fields
ContactsViewModel viewModel;
ListViewScrollView scrollview;
double previousOffset;
public SfListView listview { get; private set; }
#endregion
#region Overrides
protected override void OnAttachedTo(SfListView bindable)
{
base.OnAttachedTo(bindable);
listview = bindable as SfListView;
viewModel = new ContactsViewModel();
listview.BindingContext = viewModel;
scrollview = listview.GetScrollView();
scrollview.Scrolled += Scrollview_Scrolled;
}
protected override void OnDetachingFrom(SfListView bindable)
{
base.OnDetachingFrom(bindable);
scrollview.Scrolled -= Scrollview_Scrolled;
scrollview = null;
listview = null;
viewModel = null;
}
#endregion
#region Call back
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;
}
#endregion
}
In the above code, the previousOffset
is a variable that stores the previous scroll position. The ScrollY
property of the scrollView
object gives the current scroll position. By comparing these two values, we can determine the scroll direction.
Conclusion
I hope you enjoyed learning about how to detect scroll direction in .NET MAUI ListView.
You can refer to our .NET MAUI ListView feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET MAUI ListView example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!