我可以在Xamarin.Forms中的Run上重新排序ListView项吗?

时间:2014-08-18 13:26:56

标签: android xamarin.forms

由于listview有一个eventhandler ChildrenReordered ,我们可以在运行时重新排序列表视图中的项吗?

example here的帮助下,我创建了一个类似的视图,如下图所示。 如果可能的话,任何人都可以建议在运行时重新排序listview的方法。现在我从父项中删除listview并再次创建它,建议另一种重新排序项目的解决方法将有所帮助。

enter image description here

代码:

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
            }
            };
    }


}
}

提前致谢

1 个答案:

答案 0 :(得分:1)

是的,你可以。

您需要使用ObservableCollection而不是List&lt;&gt;

有些事情......

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);
}