繁体   English   中英

Xamarin Forms ListView 复选标记

[英]Xamarin Forms ListView Checkmark

我正在尝试在 Xamarin Forms 中实现 ListView。 我们可以检查或选择我们想要的项目的列表。 我想要一次选择一个项目。

我的 xaml 文件:

ListView x:Name="listview" ItemSelected="OnItemSelected" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
                        <StackLayout Padding="20,0,0,0"  VerticalOptions="Center"  Orientation="Vertical">
                            <Label Text="{Binding .}" YAlign="Center" FontSize="Medium"   />
                        </StackLayout>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

我的 xaml.cs 文件:

    public void OnItemSelected (object sender, SelectedItemChangedEventArgs e) {
        if (e.SelectedItem == null) return; 

        // add the checkmark in the event because the item was clicked
       // be able to check the item here

        DisplayAlert("Tapped", e.SelectedItem + " row was tapped", "OK");
        ((ListView)sender).SelectedItem = null;
    }

有更好的方法吗?

我想要这样的东西,没有字母和搜索菜单:

在此处输入图片说明

看看这个https://github.com/ricardoromo/Listview-Multiselect ,我制作了这个示例来模拟 miltiselect 列表视图。

您可以尝试使用 XLabs 复选框或单选按钮控件, 是控件示例列表,您只需要通过 NuGet 管理器下载包。

如果您只需要检查 1 个项目,请在您的点击事件中在项目被点击后进行操作。 下面是一个例子:

        async void FriendListView_ItemTapped(object sender, ItemTappedEventArgs e)
        {           
            var el = e.Item as ProfileItem;
            SelectedItem = el;
            if (e.Item != null)
            {
               await Navigation.PushAsync(new FriendProfile(el));
            }
            ((ListView)sender).SelectedItem = null; // de-select the row
        }

我已经尝试了 xamarin 形式的多选列表视图,并且它适用于 iOS 和 Android。

这花了我一些时间,所以我写了一篇关于整个原型的博客。 你可以在这里找到它 :

http://androidwithashray.blogspot.com/2018/03/multiselect-list-view-using-xamarin.html

该博客为您提供了有关使用复选框创建 Listview 和选择多个项目的完整信息。 希望这可以帮助!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM