Articles in this section
Category / Section

How to swipe the Xamarin.Forms ListView item while tapping (SfListView)?

4 mins read

The Xamarin.Forms SfListView allows you to swipe the ListViewItem in ItemTapped event. When swiping the item programmatically in the ItemTapped event, the touch handles the selection if Selection is enabled for SfListView and the next touch is handled for the swipe. You can also achieve this requirement by skipping the selection as mentioned in the following three ways.

Method 1:

XAML

Disable selection for SfListView by using SelectionMode as None. In this case, by tapping on one item, the tapped item will be swiped and tapping another item will reset the swiped item.

<syncfusion:SfListView x:Name="listView" SelectionMode="None" ItemSize="60" AllowSwiping="True" ItemsSource="{Binding ContactsInfo}">
    <syncfusion:SfListView.ItemTemplate >
        <DataTemplate>
            <Grid x:Name="grid">
            ...
            </Grid>
        </DataTemplate>
    </syncfusion:SfListView.ItemTemplate>
    <syncfusion:SfListView.LeftSwipeTemplate>
        <DataTemplate>
            <Grid BackgroundColor="Teal">
                <Label Text="Left swipe template" HorizontalOptions="Center" VerticalOptions="Center" LineBreakMode="NoWrap"/>
            </Grid>
        </DataTemplate>
    </syncfusion:SfListView.LeftSwipeTemplate>
</syncfusion:SfListView>

C#

In the ItemTapped event, swipe the ListViewItem using the SfListView.SwipeItem method.

namespace ListViewXamarin
{
    public class Behavior : Behavior<ContentPage>
    {
        SfListView ListView;
        protected override void OnAttachedTo(ContentPage bindable)
        {
            ListView = bindable.FindByName<SfListView>("listView");
            ListView.ItemTapped += ListView_ItemTapped;
            base.OnAttachedTo(bindable);
        }
 
        private void ListView_ItemTapped(object sender, Syncfusion.ListView.XForms.ItemTappedEventArgs e)
        {
            ListView.SwipeItem(e.ItemData, ListView.SwipeOffset);
        }
    }
}

Method 2:

C#

Set Handled property of the ItemTappedEventArgs as true to handle the touch by ItemTapped event. In this case, the touch will not be passed to the selection process. Also, taping another item will reset the swiped item.

namespace ListViewXamarin
{
    public class Behavior : Behavior<ContentPage>
    {
        SfListView ListView;
        protected override void OnAttachedTo(ContentPage bindable)
        {
            ListView = bindable.FindByName<SfListView>("listView");
            ListView.ItemTapped += ListView_ItemTapped;
            base.OnAttachedTo(bindable);
        }
 
        private void ListView_ItemTapped(object sender, Syncfusion.ListView.XForms.ItemTappedEventArgs e)
        {
            ListView.SwipeItem(e.ItemData, 200);
            e.Handled = true;
        }
    }
}

Method 3:

XAML

You can use GestureRecongnizers for ItemTemplate to swipe the ListViewItem. In this case, the gestures will handle the touch instead of the ListViewItem. Thus, on each interaction, the tapped item is swiped.

<syncfusion:SfListView x:Name="listView" SwipeOffset="200" ItemSize="60" AllowSwiping="True" ItemsSource="{Binding ContactsInfo}">
    <syncfusion:SfListView.ItemTemplate >
        <DataTemplate>
            <Grid x:Name="grid">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="70" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.GestureRecognizers>
                    <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
                </Grid.GestureRecognizers>
                <Image Source="{Binding ContactImage}" VerticalOptions="Center" HorizontalOptions="Center" HeightRequest="50" WidthRequest="50"/>
                <Grid Grid.Column="1" RowSpacing="1" Padding="10,0,0,0" VerticalOptions="Center">
                    <Label LineBreakMode="NoWrap" TextColor="#474747" Text="{Binding ContactName}"/>
                    <Label Grid.Row="1" Grid.Column="0" TextColor="#474747" LineBreakMode="NoWrap" Text="{Binding ContactNumber}"/>
                </Grid>
            </Grid>
        </DataTemplate>
    </syncfusion:SfListView.ItemTemplate>
    <syncfusion:SfListView.LeftSwipeTemplate>
        <DataTemplate>
            <Grid BackgroundColor="Teal">
                <Label Text="Left swipe template" HorizontalOptions="Center" VerticalOptions="Center" LineBreakMode="NoWrap"/>
            </Grid>
        </DataTemplate>
    </syncfusion:SfListView.LeftSwipeTemplate>
</syncfusion:SfListView>

C#

namespace ListViewXamarin
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
 
        private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
        {
            var currentItem = (sender as Grid).BindingContext as Contacts;
            listView.SwipeItem(currentItem, listView.SwipeOffset);
        }
    }
}

Output

Demo image to swipe ListViewItem on ItemTapped event in Xamarin.forms SfListView

View sample in GitHub


Conclusion

I hope you enjoyed learning about how to apply hover effect for ListView Item in Xamairn.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