Reputation: 1808
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.
Code:
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
}
};
}
}
}
Thanks in advance
Upvotes: 1
Views: 3538
Reputation: 11040
Yes you can.
You will need to use ObservableCollection instead of List<>
Something along the lines of...
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);
}
Upvotes: 1