Articles in this section
Category / Section

How to scroll and focus on ListView item in Xamarin.Forms (SfListView)?

1 min read

You can scroll to the particular ListViewItem and set the focus for the Entry control loaded inside the SfListView.ItemTemplate in Xamarin.Forms ListView.

XAML

Setting Behavior to ContentPage and add Button to scroll to the particular item.


<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:ListViewXamarin"
             xmlns:syncfusion="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"
             x:Class="ListViewXamarin.MainPage">
    <ContentPage.BindingContext>
        <local:ContactsViewModel/>
    </ContentPage.BindingContext>
    <ContentPage.Behaviors>
        <local:Behavior/>
    </ContentPage.Behaviors>
  <ContentPage.Content>
        <Grid RowSpacing="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Button x:Name="scrollButton" Text="Scroll to the last item"/>
            <Grid x:Name="mainGrid" Grid.Row="1" Padding="10,25,10,10">
                <syncfusion:SfListView ItemsSource="{Binding ContactsInfo}" x:Name="listView">
                    <syncfusion:SfListView.ItemTemplate>
                        <DataTemplate>
                            <Entry x:Name="entry" BackgroundColor="DarkBlue" PlaceholderColor="LightBlue" Placeholder="{Binding ContactName}"/>
                        </DataTemplate>
                    </syncfusion:SfListView.ItemTemplate>
                </syncfusion:SfListView>
            </Grid>
        </Grid>
    </ContentPage.Content>
</ContentPage>

C#

The scrolling of particular index using the ScrollToRowIndex method was handled in the Button.Clicked event. You can get the VisualContainer using the GetVisualContainer method and get the element loaded inside the ListViewItem to set Focus to the Entry control.

using Syncfusion.ListView.XForms.Control.Helpers;
namespace ListViewXamarin
{
    public class Behavior : Behavior<ContentPage>
    {
        SfListView ListView;
        Button Button;
        VisualContainer Container;
 
        protected override void OnAttachedTo(ContentPage bindable)
        {
            ListView = bindable.FindByName<SfListView>("listView");
            Button = bindable.FindByName<Button>("scrollButton");
            Container = ListView.GetVisualContainer();
            Button.Clicked += Button_Clicked;
 
            base.OnAttachedTo(bindable);
        }
 
        private async void Button_Clicked(object sender, EventArgs e)
        {
            var index = ListView.DataSource.DisplayItems.Count() - 1;
            var item = ListView.DataSource.DisplayItems[index];
            ListView.LayoutManager.ScrollToRowIndex(index, Syncfusion.ListView.XForms.ScrollToPosition.End, false);
 
            await Task.Delay(1000);
            for (int i = 0; i < Container.Children.Count; i++)
            {
                if (Container.Children[i].BindingContext == item)
                {
                    var entry = (Container.Children[i] as ListViewItem).Content as Entry;
                    entry.Focus();
                }
            }
        }
    }
}

Output

Demo image to scroll to particular index and set focus for the control in Xamarin.Forms SfListView

View sample in GitHub  


Conclusion

I hope you enjoyed learning about how to scroll and focus on ListView item in Xamarin.Forms (SfListView).

You can refer to our Xamarin.Forms ListView feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.Forms ListView documentation 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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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