Listview xamarin表单列表视图多选

Listview xamarin表单列表视图多选,listview,xamarin.forms,xamarin.android,Listview,Xamarin.forms,Xamarin.android,我有一个包含listview的xamarin.forms应用程序。我试图实现的是多选择listview并执行一些操作。当前listview包含一个项目点击事件,该事件将打开另一个页面。当用户像在其他appseg中一样持有列表视图项目单元格时,如何实现多选择listview;消息、whatsapp等。我的大致计划是,如果我以某种方式实现了长新闻事件,我将在listview中显示复选框 那么,什么是multiselect listview的最佳方法呢?是否可以在按住listview项单元格的同时启用

我有一个包含listview的xamarin.forms应用程序。我试图实现的是多选择listview并执行一些操作。当前listview包含一个项目点击事件,该事件将打开另一个页面。当用户像在其他appseg中一样持有列表视图项目单元格时,如何实现多选择listview;消息、whatsapp等。我的大致计划是,如果我以某种方式实现了长新闻事件,我将在listview中显示复选框

那么,什么是multiselect listview的最佳方法呢?是否可以在按住listview项单元格的同时启用它

感谢您的帮助

编辑1:我按照Jason的建议使用了Collection视图。它提供了单个和多个选择模式,运行良好。但是,当用户按住单个单元格时,如何将集合视图的选择模式从单个更改为多个

编辑2:正如LeonLu MSFT对长新闻事件的建议,我使用了alexdunn.org的效果。但我没有使用MVVM模式。我是在代码隐藏中实现它的,所以根据alexdunn.org的文章,它是根据命令工作的。如何使用命令在后端c上获取长按事件

我的列表视图具有长按效果


如何在后端获取该命令?

您不能直接通过Portable实现,但可以通过特定于平台的方式实现

注意:对于多选复选框,您可以在模型类中创建属性,如IsSelected,然后基于此属性,选中复选框并取消选中

在ListView中创建CustomView以呈现模板,如下所示,在MVVM中公开一个用于处理保持操作的命令。以及用于从渲染器提升保持动作的接口

公共接口ICustomViewController:IViewController { 无效诉讼; } 公共类CustomView:ContentView、ICustomViewController { 公众自定义视图 { } 公共静态只读BindableProperty HoldCommandProperty=BindableProperty.CreateHoldCommand,命令类型,自定义视图类型,null; 公共命令保持命令 { 获取{return CommandGetValueHoldCommandProperty;} set{SetValueHoldCommandProperty,value;} } 公共无效诉讼 { HoldCommand.Executethis.BindingContext; } } 以及自定义视图的XAML部分,如下所示

下面是Android的渲染器部分

公共类CustomViewRenderer:ViewRenderer { 公共CustomViewRenderContext上下文:basecontext { } 受保护的覆盖无效OnElementChangedElementChangedEventArgs e { base.OnElementChangede; 如果e.NewElement!=null { 如果this.Control==null this.setNativeControl新建Android.Views.Viewthis.Context; 如果控制!=null { Control.LongClickable=true; Control.setonLongClickListener新内容视图LongClickListener此; } } } 受保护的覆盖无效处置本处置 { 如果处置 { 如果此.Control!=null { Control.SetOnLongClickListenernull; } } 基础、处置; } 私有类ContentViewLongClickListener:Java.Lang.Object,IOnLongClickListener { 私有只读CustomViewRenderer viewRenderer; 公共内容视图长单击列表器customViewRenderer customViewRenderer { viewRenderer=customViewRenderer; } public bool OnLongClickAndroid.Views.View v { viewRenderer?.Element?.SendHoldAction; 返回true; } } }
同样,您也可以在iOS上实现它。

要获得任何控件的长按事件,请在名为CustomView的共享代码中创建一个类


获得此答案。您可以将boxview添加到布局中。单击时,更改颜色,就像它被选中一样。您也可以直接更改布局的颜色,无需添加boxview。然后用按钮处理所有选定的条形图。

使用CollectionView-@Jason Hi,感谢您提供有关collection view的信息。当用户点击并按住视图单元格时,我如何进行选择?如果您不想使用自定义渲染器来实现长点击事件,请等待此功能。您也可以尝试此解决方法@LeonLu MSFT我使用了alexdunn.org的Xamarin.Forms长按效果。但是我没有使用MVVM。我无法在后端cs上获取单击事件。你能帮我吗?谢谢甘尼桑的回答+1.我用另一种方法做的
 <ListView  x:Name="TimesheetListView"  ItemsSource="{Binding} " 
              HasUnevenRows="True"                                                                        
              HeightRequest="{Binding Path=Height, Source={x:Reference ListLayout}}"
              CachingStrategy="RecycleElement"
              SeparatorVisibility="None"                                       
              BackgroundColor="Transparent"                      
              HorizontalOptions="FillAndExpand"                        
              VerticalOptions="FillAndExpand">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <ViewCell.View>
                                    <Frame  BorderColor="LightGray" 
                                    CornerRadius="5" BackgroundColor="White" 
                                    Margin="2" Padding="5" HasShadow="False"
                                    Text="Long Press Me!" effects:LongPressedEffect.Command="{Binding ShowAlertCommand}"
                                    effects:LongPressedEffect.CommandParameter="{Binding .}"
                                    >
                                                         <Frame.Effects>
                                    <effects:LongPressedEffect />
                                    </Frame.Effects>        
                                    <Label Text="Lognpress" FontSize="Micro" TextColor="Black"  VerticalOptions="Center">
                                    </Label>                                
                                    </Frame>
                                </ViewCell.View>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
public class CustomView: ContentView
    {
        public event EventHandler<EventArgs> LongPressEvent;

        public void RaiseLongPressEvent()
        {
            if (IsEnabled)
            LongPressEvent?.Invoke(this, EventArgs.Empty);
        }

    }
 public class LongTouchCustomRender : ViewRenderer<CustomView, Android.Views.View>
    {
        private CustomViewListener _listener;
        private GestureDetector _detector;

        public CustomViewListener Listener
        {
            get
            {
                return _listener;
            }
        }

        public GestureDetector Detector
        {
            get
            {
                return _detector;
            }
        }

        protected override void OnElementChanged(ElementChangedEventArgs<CustomView> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement == null)
            {
                GenericMotion += HandleGenericMotion;
                Touch += HandleTouch;

                _listener = new CustomViewListener(Element);
                _detector = new GestureDetector(_listener);
            }
        }

        protected override void Dispose(bool disposing)
        {
            GenericMotion -= HandleGenericMotion;
            Touch -= HandleTouch;

            _listener = null;
            _detector?.Dispose();
            _detector = null;

            base.Dispose(disposing);
        }

        void HandleTouch(object sender, TouchEventArgs e)
        {
            _detector.OnTouchEvent(e.Event);
        }

        void HandleGenericMotion(object sender, GenericMotionEventArgs e)
        {
            _detector.OnTouchEvent(e.Event);
        }
    }

    public class CustomViewListener : GestureDetector.SimpleOnGestureListener
    {
        readonly CustomView _target;

        public CustomViewListener(CustomView s)
        {
            _target = s;
        }

        public override void OnLongPress(MotionEvent e)
        {
            _target.RaiseLongPressEvent();

            base.OnLongPress(e);
        }
    }
  public class LongTouchCustomRender : ViewRenderer<CustomView, UIView>
        {
            UILongPressGestureRecognizer longPressGestureRecognizer;
            protected override void OnElementChanged(ElementChangedEventArgs<CustomView> e)
            {
                longPressGestureRecognizer = longPressGestureRecognizer ??
                    new UILongPressGestureRecognizer(() =>
                    {
                        Element.RaiseLongPressEvent();
                    });

                if (longPressGestureRecognizer != null)
                {
                    if (e.NewElement == null)
                    {
                        this.RemoveGestureRecognizer(longPressGestureRecognizer);
                    }
                    else if (e.OldElement == null)
                    {
                        this.AddGestureRecognizer(longPressGestureRecognizer);
                    }
                }
            }
        }
<local:CustomView LongPressEvent="Handle_LongPress" />
void Handle_LongPressEvent(object sender, System.EventArgs e)
{
    //handle long press event here
}