How to detect a swipe event on ListView in Android?

storstadstorstad Member ✭✭

Hello,

In iOS, I use a GestureRecognizer to detect swiping. When I swipe from left to the right, another page appears (et vice versa).
I explicitly do not want to implement that I can manage the current entry I swipe on. e. g.

I want do not want to move the current touched entry. I only want to detect a swipe from right to the left and switch to another independent page.

The problem is that only iOS supports something like that natively.
There is a library named SimpleListView (for Android) I tried, but it is deprecated and does not support the current mono library.

Is there an easy way to achieve the same behavior in Android like in iOS?

Thanks in advance.

Answers

  • yelinzhyelinzh Member, Xamarin Team Xamurai

    Swipe to delete is now built into Xamarin Froms ListViews using a ContextAction. Context Actions are created using MenuItems. Tap events for MenuItems are raised by the MenuItem itself, not the ListView.
    The effect is as shown, you can refer to the code.
    page.xaml

    <ListView x:Name="listview">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.ContextActions>
                        <MenuItem Clicked="OnMore"  Text="More" CommandParameter="{Binding .}"/>
                        <MenuItem Clicked="OnDelete" Text="Delete" IsDestructive="True" CommandParameter="{Binding .}" />
                    </ViewCell.ContextActions>
                    <Label Text="{Binding content}" VerticalOptions="Center" HorizontalOptions="Center"/>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    page.xaml.cs

    public partial class Page4 : ContentPage
    {
        ObservableCollection<model_4> list = new ObservableCollection<model_4>();
        public Page4()
        {
            InitializeComponent();
    
            list.Add(new model_4 { content = "content_1" });
            list.Add(new model_4 { content = "content_2" });
            list.Add(new model_4 { content = "content_3" });
            list.Add(new model_4 { content = "content_4" });
            list.Add(new model_4 { content = "content_5" });
            listview.ItemsSource = list;
        }
        private void OnMore(object sender, EventArgs e)
        {
        //
        }
        private void OnDelete(object sender, EventArgs e)
        {
            var model = ((MenuItem)sender);
            list.Remove((model_4)model.CommandParameter);
        }
    }
    
    public class model_4
    {
        public string content { get; set; }
    }
    

  • storstadstorstad Member ✭✭

    @yelinzh Thanks but I explicitly said I do not want to delete an item or to do item specific interaction.
    I only want to detect that the user swipe from to any side to switch the page (without doing sth with the listview itself)

  • yelinzhyelinzh Member, Xamarin Team Xamurai

    Have you tried adding a swipe gesture recognizer to ListView?
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/gestures/swipe

  • storstadstorstad Member ✭✭

    @yelinzh Yes, it doesn't work or rather the Android ListView does not support the SwipeGestureRecognizer.

  • yelinzhyelinzh Member, Xamarin Team Xamurai

    You can try SfListView which contains multiple events that you can use to detect Swipe status.
    Remember to set SfListView.AllowSwiping to True.

    Tutorial: https://help.syncfusion.com/xamarin/sflistview/swiping
    A basice demo, you can refer to the code.

  • storstadstorstad Member ✭✭

    Thank you but this is not what I want. I do not want to interact with the single entries. I just want to detect that the user wants to go to e.g. settings page by swipe events.

Sign In or Register to comment.