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 swipe an item programatically in Xamarin.Forms ListView

Platform: Xamarin.Forms |
Control: SfListView |
Published Date: December 17, 2018 |
Last Revised Date: February 27, 2020

You can swipe an item programmatically by using the SwipeItem method of Xamarin.Forms SfListView.

 

XAML

 

<ContentPage.Content>
    <StackLayout>
        <Grid HeightRequest="50">
            <Button x:Name="RightSwipe" Text="Right Swipe Button" />
            <Button x:Name="LeftSwipe" Text="Left Swipe Button" Grid.Column="1"/>
        </Grid>
        <listView:SfListView x:Name="listView" ItemSize="70" SelectionMode="Single" AllowSwiping="True"
    ItemSpacing="0,0,5,0" >
            <listView:SfListView.LeftSwipeTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <Grid BackgroundColor="SlateBlue"  HorizontalOptions="Fill" VerticalOptions="Fill">
                                <Label Text="Left Swipe Template" TextColor="White" VerticalOptions="Center" HorizontalOptions="Center"/>
                            </Grid>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </listView:SfListView.LeftSwipeTemplate>
            <listView:SfListView.RightSwipeTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <Grid BackgroundColor="SlateBlue" HorizontalOptions="Fill" VerticalOptions="Fill">
                                <Label Text="Right Swipe Template" TextColor="White" VerticalOptions="Center"/>
                            </Grid>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </listView:SfListView.RightSwipeTemplate>
        </listView:SfListView>
    </StackLayout>
</ContentPage.Content>

 

C#

 

You need to pass the item to be swiped and SwipeOffset as parameter in the SwipeItem method. The SwipeOffset value should be positive for LeftSwiping of ListViewItem.

 

private void LeftSwipeButton_Clicked(object sender, EventArgs e)
{
    ListView.SwipeItem(viewModel.contactsinfo[1], 200);
}

 

The SwipeOffset value should be negative for RightSwiping of ListViewItem.

 

private void RightSwipeButton_Clicked(object sender, EventArgs e)
{
    ListView.SwipeItem(viewModel.contactsinfo[1], -150);
}

 

Screenshot

 

LeftSwipe image for Xamarin.Forms ListView   RightSwipe image for Xamarin.Forms ListView

 

Download sample in 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