【发布时间】:2020-05-31 04:44:10
【问题描述】:
我正在寻找 ListView 上的跨平台滑动来执行诸如打开菜单或删除项目之类的操作。
有没有人实现过这样的场景?例如,我想在向左滑动项目时删除一个项目。
我需要支持所有平台:ios/droid/win
【问题讨论】:
标签: xamarin xamarin.forms
我正在寻找 ListView 上的跨平台滑动来执行诸如打开菜单或删除项目之类的操作。
有没有人实现过这样的场景?例如,我想在向左滑动项目时删除一个项目。
我需要支持所有平台:ios/droid/win
【问题讨论】:
标签: xamarin xamarin.forms
最简单的方法是使用内置的上下文操作,这允许您在列表项在 iOS 上滑动或在 Android 上长按时显示菜单项
<ListView x:Name="ContextDemoList">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Clicked="OnMore" CommandParameter="{Binding .}"
Text="More" />
<MenuItem Clicked="OnDelete" CommandParameter="{Binding .}"
Text="Delete" IsDestructive="True" />
</ViewCell.ContextActions>
<StackLayout Padding="15,0">
<Label Text="{Binding title}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
如果两个平台都需要滑动,或者您需要在滑动项目时执行操作(而不是显示菜单项),那么我推荐 SyncFucion 的列表视图控件,它可以通过社区许可证获得,它提供更灵活
<syncfusion:SfListView x:Name="listView" AllowSwiping="True">
<syncfusion:SfListView.LeftSwipeTemplate>
<DataTemplate x:Name="LeftSwipeTemplate">
<Grid>
<Grid BackgroundColor="#009EDA" HorizontalOptions="Fill" VerticalOptions="Fill" Grid.Column="0">
<Grid VerticalOptions="Center" HorizontalOptions="Center">
<Image Grid.Column="0"
Grid.Row="0"
BackgroundColor="Transparent"
HeightRequest="35"
WidthRequest="35"
Source="Favorites.png" />
</Grid>
</Grid>
</Grid>
</DataTemplate>
</syncfusion:SfListView.LeftSwipeTemplate>
</syncfusion:SfListView>
【讨论】:
您可以使用 SwipeView 并将其包装在 ListView 中。
<ListView
ItemsSource="{Binding Items}"
SelectionMode="None"
CachingStrategy="RecycleElement"
RowHeight="110"
HeightRequest="1000"
BackgroundColor="White"
IsPullToRefreshEnabled="True"
Refreshing="OnRefresh">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<SwipeView>
<SwipeView.GestureRecognizers>
<TapGestureRecognizer
Tapped="OnTapped"
NumberOfTapsRequired="1" />
</SwipeView.GestureRecognizers>
<SwipeView.RightItems>
<SwipeItems>
<SwipeItem
Text="Edit"
IconImageSource="edit_icon.png"
BackgroundColor="Green"
Invoked="OnEdit" />
<SwipeItem
Text="Delete"
IconImageSource="delete_icon.png"
BackgroundColor="Red"
Invoked="OnDelete" />
</SwipeItems>
</SwipeView.RightItems>
<SwipeView.Content>
:
</SwipeView.Content>
</SwipeView>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
【讨论】: