This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
interactiveListViewCode.cs
executable file
·105 lines (89 loc) · 2.96 KB
/
interactiveListViewCode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.ObjectModel;
using System.Linq;
using Xamarin.Forms;
namespace interactivityListView
{
public class interactiveListViewCode : ContentPage
{
public static ObservableCollection<string> items { get; set; }
public interactiveListViewCode ()
{
items = new ObservableCollection<string> () { "Speaker", "Pen", "Lamp", "Monitor", "Bag", "Book", "Cap", "Tote", "Floss", "Phone"};
ListView lstView = new ListView();
lstView.IsPullToRefreshEnabled = true;
lstView.Refreshing += OnRefresh;
lstView.ItemSelected += OnSelection;
lstView.ItemTapped += OnTap;
lstView.ItemsSource = items;
//lstView.SelectionMode = ListViewSelectionMode.None;
var temp = new DataTemplate (typeof(textViewCell));
lstView.ItemTemplate = temp;
Content = new StackLayout
{
Margin = new Thickness(20),
Children =
{
new Label { Text = "ListView Interactivity", FontAttributes = FontAttributes.Bold, HorizontalOptions = LayoutOptions.Center },
lstView
}
};
}
void OnTap (object sender, ItemTappedEventArgs e)
{
DisplayAlert ("Item Tapped", e.Item.ToString (), "Ok");
}
void OnSelection (object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem == null)
{
return;
}
DisplayAlert ("Item Selected", e.SelectedItem.ToString (), "Ok");
}
void OnRefresh (object sender, EventArgs e)
{
var list = (ListView)sender;
//put your refreshing logic here
var itemList = items.Reverse().ToList();
items.Clear ();
foreach (var s in itemList)
{
items.Add (s);
}
//make sure to end the refresh state
list.IsRefreshing = false;
}
}
public class textViewCell : ViewCell
{
public textViewCell()
{
StackLayout layout = new StackLayout ();
layout.Padding = new Thickness (15, 0);
Label label = new Label ();
label.SetBinding (Label.TextProperty, ".");
layout.Children.Add (label);
var moreAction = new MenuItem { Text = "More" };
moreAction.SetBinding (MenuItem.CommandParameterProperty, new Binding ("."));
moreAction.Clicked += OnMore;
var deleteAction = new MenuItem { Text = "Delete", IsDestructive = true }; // red background
deleteAction.SetBinding (MenuItem.CommandParameterProperty, new Binding ("."));
deleteAction.Clicked += OnDelete;
this.ContextActions.Add (moreAction);
this.ContextActions.Add (deleteAction);
View = layout;
}
void OnMore (object sender, EventArgs e)
{
var item = (MenuItem)sender;
//Do something here... e.g. Navigation.pushAsync(new specialPage(item.commandParameter));
//page.DisplayAlert("More Context Action", item.CommandParameter + " more context action", "OK");
}
void OnDelete (object sender, EventArgs e)
{
var item = (MenuItem)sender;
interactiveListViewCode.items.Remove (item.CommandParameter.ToString());
}
}
}