Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

SwipeGestureRecognizer in a ListView

$
0
0

Hi,
I'm trying to use a SwipeGestureRecognizer in a ListView so that I can swipe a row, and execute an action on that row. Unfortunately I can't get it to work as I would like. I have the following ListView definition.

            <ListView x:Name="todoList"
                      IsPullToRefreshEnabled="true" 
                      Refreshing="OnRefresh" 
                      SeparatorVisibility="Default"
                      Grid.Row="1">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ViewCell.ContextActions>
                                <MenuItem Clicked="OnComplete" Text="Complete" CommandParameter="{Binding .}"/>
                                <MenuItem Clicked="OnDelete" Text="Delete" IsDestructive="True" CommandParameter="{Binding .}" />
                            </ViewCell.ContextActions>
                            <StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal" Padding="15,5,10,10">
                                    <Label Text="{Binding Name}">
                                        <Label.GestureRecognizers>
                                            <SwipeGestureRecognizer Direction="Left" Swiped="OnSwiped" CommandParameter="{Binding .}"/>
                                            <SwipeGestureRecognizer Direction="Right" Swiped="OnSwiped" CommandParameter="{Binding .}"/>
                                        </Label.GestureRecognizers>
                                    </Label>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

If I put the SwipeGestureRecognizer in the ListView i.e. just above ListView.ItemTemplate, it calls the OnSwipe method, but the SwipedEventArgs.Parameter is always null, so I can't determine which row was swiped. If I put the SwipeGestureRecognizer in the Label (as displayed) it calls OnSwipe, and the Parameter has the correct value, however, it only works if the actual label is swiped; if I swipe anywhere else in the row, OnSwipe doesn't get called. I've also tried putting it inside the StackLayout, but then OnSwipe isn't called at all. I would have thought it should go in the ViewCell, but that doesn't compile.

There is a (very old) Forum question about this, but it recommends using ContextActions, but they just seem to use MenuItems, not swipe gestures. Is there a way to swipe anywhere on a ListView row, and get it to call OnSwipe with the required values in SwipedEventArgs.Parameter (or is there an alternative to this).


Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>