Xamarin 形成 SFListview 多选问题

Xamarin forms SFListview multiselect issue

我在我的 xamarin 表单应用程序中使用 syncfusion SFListview。我从 https://help.syncfusion.com/xamarin/sflistview/selection?cs-save-lang=1&cs-lang=xaml.It 实现了列表视图单元格的多选 fine.But 我面临的问题是每次我们需要保留项目单元格以供选择。多选是否可能只保留第一个单元格并点击所有其他单元格?

Is it possible for multiselect that hold only for first cell and tap for all other cell?

当然可以。如果你想要多选项目,我想接下来的步骤会做一些关于多选的任务items.The下面的图片可能看起来像你想要。

你可以看看this chapter in the share link, and the sample code它提供的内容

方案一:(一般可以接受)

如果项目不介意在外面加一个控制按钮,那么这个最快最简单way.That就是在NavigationPage里面加一个ToolbarItems,用吧来控制是否可以点击多选而不跳转到下一页。

添加ToolbarItems

<ContentPage.ToolbarItems>
        <ToolbarItem x:Name="ToolbarItemsButton" Text="MultipleSelect" Clicked="Edit_Clicked"/>
</ContentPage.ToolbarItems>

<sync:SfListView x:Name="listView"
                 SelectionGesture="Hold"
                 SelectionMode="Multiple"
                 ItemTapped="ListView_ItemTapped"
                 SelectionBackgroundColor="Transparent"
                 IsStickyHeader="True" ItemSize="70">
 ...

ContentPage中添加Flag判断ListViewSelectionMode

int flag = 0;

private void Edit_Clicked(object sender, EventArgs e)
{
    if(0 == flag)
    {
        listView.SelectionGesture = TouchGesture.Tap;
        ToolbarItemsButton.Text = "Done";
        flag = 1;
    }
    else
    {
        ToolbarItemsButton.Text = "MultipleSelect";
        listView.SelectionGesture = TouchGesture.Hold;
        flag = 0;
    }
}

可以判断什么时候可以切换到下一页

private void ListView_ItemTapped(object sender, Syncfusion.ListView.XForms.ItemTappedEventArgs e)
{

    if(0 == flag)
    {
        Navigation.PushAsync(new ContentPage());
    }

}

方案二:(推荐)

SfListView有一个方法是ItemHolding。不用别的按钮也可以换成SelectionMode

Xaml代码不同的是添加了SfListView.

这个方法
<sync:SfListView x:Name="listView"
                     SelectionGesture="Hold"
                     SelectionMode="Multiple"
                     ItemTapped="ListView_ItemTapped"
                     SelectionBackgroundColor="Transparent"
                     ItemHolding="ListView_ItemHolding" // ItemHolding
                     IsStickyHeader="True" ItemSize="70">

OnHolding 什么时候可以在这里做点什么:

private void ListView_ItemHolding(object sender, ItemHoldingEventArgs e)
{
    if (0 == flag)
    {
        listView.SelectionGesture = TouchGesture.Tap;
        ToolbarItemsButton.Text = "Done";
        flag = 1;
    }
    else
    {
        listView.SelectionGesture = TouchGesture.Hold;
        ToolbarItemsButton.Text = "MultipleSelect";
        flag = 0;
    }
}

判断什么时候可以切换到下一页

private void ListView_ItemTapped(object sender, Syncfusion.ListView.XForms.ItemTappedEventArgs e)
{

    if(0 == flag)
    {
        Navigation.PushAsync(new ContentPage());
    }

}

方案三:(这里不推荐)

一般对于listview的cell的多选,我们会处理自定义cell的模板,比如在模板中添加一个按钮。点击时可以将item标记为选中,也可以将item的UI自定义为选中时的样式