We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to add a jump list with Xamarin.Forms ListView

Platform: Xamarin.Forms |
Control: SfListView |
Published Date: February 24, 2020 |
Last Revised Date: February 24, 2020

You can add jump list using Xamarin.Forms ListView by tapping on the indexed letter (group key) and scrolling to the respective group (GroupHeaderItem) by passing group key index in the ScrollToIndex method aslike contact list. This article explains you how to create jump list with group key and scroll to the tapped item.

Creates panel to shows the group key as indexed vertically.

<ContentPage xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms" >
    <ContentPage.Content>
       <Grid >
          <syncfusion:SfListView x:Name="listView" Grid.Row="0">
                <syncfusion:SfListView.ItemTemplate>
                          <DataTemplate>
                                     <ViewCell>
                                             <Grid>
                                                  <Image Source="{Binding ContactImage}" Grid.Column="0"/>
                                                        <StackLayout Orientation="Vertical" Grid.Column="1">
                                                                     <Label Text="{Binding ContactName}"/>
                                                                     <Label Text="{Binding ContactNumber}"/>
                                                        </StackLayout>
                                               </Grid>
                                             <StackLayout HeightRequest="1" BackgroundColor="LightGray"/>
                                     </ViewCell>
                          </DataTemplate>
                </syncfusion:SfListView.ItemTemplate>                
            </syncfusion:SfListView>
            <Grid x:Name="IndexPanelGrid" Grid.Row="0"  VerticalOptions="CenterAndExpand" HorizontalOptions="End" />
         </Grid>
    </ContentPage.Content>
</ContentPage>

 

Creates and populates the index panel with index labeled from the Group key.

ListView.Loaded += ListView_Loaded;
 
private void ListView_Loaded(object sender, ListViewLoadedEventArgs e)
{
     var groupcount = this.ListView.DataSource.Groups.Count;
     for (int i = 0; i < groupcount; i++)
     {
        label = new Label();  
        GroupResult group = ListView.DataSource.Groups[i];
        label.Text = group.Key.ToString();
        indexPanelGrid.Children.Add(label, 0, i);
        var labelTapped = new TapGestureRecognizer() { Command = new Command<object>(OnTapped), CommandParameter = label };
        label.GestureRecognizers.Add(labelTapped);
     }
}

 

ByOn tapping the group key value loaded in the index panel, you can scroll the listview to the respective group by comparing all GroupHeader’s Key values as follows.like below.

private void OnTapped(object obj)
{
    if (previousLabel != null)
    {
      previousLabel.TextColor = Color.DimGray;
    }
    var label = obj as Label;
    var text = label.Text;
    label.TextColor = Color.Red;
    for (int i = 0; i < ListView.DataSource.Groups.Count; i++)
    {
        var group = ListView.DataSource.Groups[i];
 
        if (group.Key == null)
          App.Current.MainPage.DisplayAlert("Message", "Group not available", "ok");
        if ((group.Key != null && group.Key.Equals(text)))
        {
                  ListView.LayoutManager.ScrollToRowIndex (ListView.DataSource.DisplayItems.IndexOf(group), true);
         }
     }
     previousLabel = label;
}

 

OutpPut

Jump List

Download the sample from GitHub.

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile