【发布时间】:2015-11-25 00:49:37
【问题描述】:
由于列表视图有一个事件处理程序ChildrenReordered,我们可以在运行时重新排序列表视图中的项目吗??
在example 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
}
};
}
}
}
提前致谢
【问题讨论】:
标签: android xamarin.forms