我可以在Xamarin.Forms中的“运行"上对ListView项重新排序吗? [英] Can I reorder the ListView items on the Run in Xamarin.Forms?

查看:130
本文介绍了我可以在Xamarin.Forms中的“运行"上对ListView项重新排序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于listview具有事件处理程序 ChildrenReordered ,我们可以在运行时对列表视图中的项目进行重新排序吗?

Since listview has a eventhandler ChildrenReordered can we reorder the items in the list view during runtime??

借助此此处的示例与下图类似的视图. 任何人都可以建议一种在可能的情况下重新排列列表视图的方式的方法.现在,我从父级列表中删除了列表视图,然后再次创建它,建议使用其他解决方法来重新排序项目将很有帮助.

With the help of this example here I have created a similar view as in the image given below. Can anyone suggest a way to reorder listview on the run if possible. Now I remove the listview from the parent and create it again, suggestion for another workaround for reordering the items will be helpful.

代码:

using System;
using System.Linq;
using System.Collections.Generic;
using Xamarin.Forms;

namespace OrderBy
{
class ListViewDemoPage : ContentPage
{
    List<SourceItem> people = null;

    class SourceItem
    {
        public SourceItem(string Item)
        {
            this.Item = Item;
        }

        public string Item { private set; get; }
    };

    public ListViewDemoPage()
    {

        // Define some data.
        people = new List<SourceItem>
        {
            new SourceItem("Abigail"),
            new SourceItem("Bob"),
            new SourceItem("Cathy"),
            new SourceItem("David"),
            new SourceItem("Eugenie"),
            new SourceItem("Freddie"),
            new SourceItem("Greta"),
            new SourceItem("Harold"),
            new SourceItem("Irene"),
            new SourceItem("Jonathan"),
            new SourceItem("Kathy"),
            new SourceItem("Larry"),
            new SourceItem("Monica"),
            new SourceItem("Nick"),
            new SourceItem("Olive"),
            new SourceItem("Pendleton"),
            new SourceItem("Queenie"),
            new SourceItem("Rob"),
            new SourceItem("Sally"),
            new SourceItem("Timothy"),
            new SourceItem("Uma"),
            new SourceItem("Victor"),
            new SourceItem("Wendy"),
            new SourceItem("Xavier"),
            new SourceItem("Yvonne"),
            new SourceItem("Zachary")
        };

        // Create the ListView.
        ListView listView = new ListView
        {
            // Source of data items.
            ItemsSource = people,
            WidthRequest = 300,
            HeightRequest = 350,

            ItemTemplate = new DataTemplate(() =>
                {
                    // Create views with bindings for displaying each property.
                    Label ItemLabel = new Label();
                    ItemLabel.WidthRequest = 200;
                    ItemLabel.SetBinding(Label.TextProperty, "Item");

                    Image up = new Image();
                    up.Source = ImageSource.FromFile("up_arrow.png");
                    up.BackgroundColor = Color.Transparent;
                    up.WidthRequest = 25;
                    up.HeightRequest = 25;

                    Image down = new Image();
                    down.Source = ImageSource.FromFile("down_arrow.png");
                    down.BackgroundColor = Color.Transparent;
                    down.WidthRequest = 25;
                    down.HeightRequest = 25;

                    // Return an assembled ViewCell.
                    return new ViewCell
                    {
                        View = new StackLayout
                        {
                            Padding = new Thickness(0, 5),
                            Orientation = StackOrientation.Horizontal,
                            Children = 
                            {
                                ItemLabel,
                                up, down,

                            }
                            }
                    };
                })
        };

        // Accomodate iPhone status bar.
        this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);

        // Build the page.
        this.Content = new StackLayout
        {
            VerticalOptions = LayoutOptions.CenterAndExpand,
            HorizontalOptions = LayoutOptions.Center,
            Children = 
            {
                listView
            }
            };
    }


}
}

预先感谢

推荐答案

是的.

您将需要使用ObservableCollection而不是List<>

You will need to use ObservableCollection instead of List<>

类似...

ObservableCollection<SourceItem> source = new ... // class property or field

ViewCell viewCell = new ...
...
Image down = new...
down.GestureRegognizers.Add(new TapGestureRecognizer(()=>MoveDown(viewCell.BindingContext as SourceItem)));

// obviously missing a bunch of checks below such as i==0
private void MoveDown(SourceItem item){
  int i = source.IndexOf(item);
  source.RemoveAt(i);
  source.Insert(item, i-1);
}

这篇关于我可以在Xamarin.Forms中的“运行"上对ListView项重新排序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆