【问题标题】:Cross platform listview swipe in xamarin formsxamarin 表单中的跨平台列表视图滑动
【发布时间】:2020-05-31 04:44:10
【问题描述】:

我正在寻找 ListView 上的跨平台滑动来执行诸如打开菜单或删除项目之类的操作。

有没有人实现过这样的场景?例如,我想在向左滑动项目时删除一个项目。

我需要支持所有平台:ios/droid/win

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    最简单的方法是使用内置的上下文操作,这允许您在列表项在 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>
    

    https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/interactivity/#Context_Actions

    如果两个平台都需要滑动,或者您需要在滑动项目时执行操作(而不是显示菜单项),那么我推荐 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>
    

    https://help.syncfusion.com/xamarin/sflistview/swiping

    【讨论】:

    • 感谢您的建议。我试图找到比购买控件许可证更开源的东西。尝试在两个平台上保持滑动
    【解决方案2】:

    您可以使用 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>
    

    发件人:https://github.com/xamarin/Xamarin.Forms/issues/9089

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 2020-01-12
      • 1970-01-01
      • 2015-12-04
      • 2017-12-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多