summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls/GalleryPages/ListViewDemoPage.cs
blob: 370fb6d94e821e615a4ab2261bbc9b841fc5bbbb (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Xamarin.Forms.Controls
{
	internal class ListViewDemoPage : ContentPage
    {
		class Person
        {
            public Person(string name, DateTime birthday, Color favoriteColor)
            {
                Name = name;
                Birthday = birthday;
                FavoriteColor = favoriteColor;
            }

            public string Name { private set; get; }

            public DateTime Birthday { private set; get; }

            public Color FavoriteColor { private set; get; }
        };

		class MyDataTemplateSelector : DataTemplateSelector
	    {
		    DataTemplate _oddTemplate;
		    DataTemplate _evenTemplate;

		    public MyDataTemplateSelector ()
		    {
			    _evenTemplate = new DataTemplate (() => {
				    // Create views with bindings for displaying each property.
				    Label nameLabel = new Label ();
				    nameLabel.SetBinding (Label.TextProperty, "Name");

				    Label birthdayLabel = new Label ();
				    birthdayLabel.SetBinding (Label.TextProperty,
											  new Binding ("Birthday", BindingMode.OneWay,
														   null, null, "Born {0:d}"));

				    BoxView boxView = new BoxView ();
				    boxView.SetBinding (BoxView.ColorProperty, "FavoriteColor");

				    // Return an assembled ViewCell.
				    return new ViewCell {
					    View = new StackLayout {
						    Padding = new Thickness (0, 5),
						    Orientation = StackOrientation.Horizontal,
						    Children = {
							    new Image {
								    HeightRequest = 40,
								    WidthRequest = 40,
								    Source = new UriImageSource {
									    //											CacheValidity = TimeSpan.FromSeconds (10),
									    Uri = new Uri ("https://xamarin.com/content/images/pages/index/xamarin-studio-icon.png"),
								    }
							    },
							    boxView,
							    new StackLayout {
								    VerticalOptions = LayoutOptions.Center,
								    Spacing = 0,
								    Children = {
									    nameLabel,
									    birthdayLabel
								    }
							    }
						    }
					    }
				    };
			    });

			    _oddTemplate = new DataTemplate (() => {
				    // Create views with bindings for displaying each property.
				    Label nameLabel = new Label ();
				    nameLabel.SetBinding (Label.TextProperty, "Name");

				    Label birthdayLabel = new Label ();
				    birthdayLabel.SetBinding (Label.TextProperty,
											  new Binding ("Birthday", BindingMode.OneWay,
														   null, null, "Born {0:d}"));

				    BoxView boxView = new BoxView ();
				    boxView.SetBinding (BoxView.ColorProperty, "FavoriteColor");

				    // Return an assembled ViewCell.
				    return new ViewCell {
					    View = new StackLayout {
						    Padding = new Thickness (0, 5),
						    Orientation = StackOrientation.Horizontal,
						    Children = {
							    new Image {
								    HeightRequest = 40,
								    WidthRequest = 40,
								    Source = new UriImageSource {
									    //											CacheValidity = TimeSpan.FromSeconds (10),
									    Uri = new Uri ("https://xamarin.com/content/images/pages/index/xamarin-studio-icon.png"),
								    }
							    },

							    new StackLayout {
								    VerticalOptions = LayoutOptions.Center,
								    Spacing = 0,
								    Children = {
									    birthdayLabel,
									    nameLabel,
								    }
							    },
							    boxView,
						    }
					    }
				    };
			    });
		    }

		    protected override DataTemplate OnSelectTemplate (object item, BindableObject container)
		    {
			    return ((Person)item).Birthday.Month % 2 == 0 ? _evenTemplate : _oddTemplate;
		    }
	    }

        public ListViewDemoPage()
        {
            Label header = new Label
            {
                Text = "ListView",
#pragma warning disable 618
                Font = Font.BoldSystemFontOfSize(50),
#pragma warning restore 618
                HorizontalOptions = LayoutOptions.Center
            };

            // Define some data.
            List<Person> people = new List<Person>
            {
                new Person("Abigail", new DateTime(1975, 1, 15), Color.Aqua),
                new Person("Bob", new DateTime(1976, 2, 20), Color.Black),
                new Person("Cathy", new DateTime(1977, 3, 10), Color.Blue),
#pragma warning disable 618
                new Person("David", new DateTime(1978, 4, 25), Color.Fuschia),
#pragma warning restore 618
                new Person("Eugenie", new DateTime(1979, 5, 5), Color.Gray),
                new Person("Freddie", new DateTime(1980, 6, 30), Color.Green),
                new Person("Greta", new DateTime(1981, 7, 15), Color.Lime),
                new Person("Harold", new DateTime(1982, 8, 10), Color.Maroon),
                new Person("Irene", new DateTime(1983, 9, 25), Color.Navy),
                new Person("Jonathan", new DateTime(1984, 10, 10), Color.Olive),
                new Person("Kathy", new DateTime(1985, 11, 20), Color.Purple),
                new Person("Larry", new DateTime(1986, 12, 5), Color.Red),
                new Person("Monica", new DateTime(1975, 1, 5), Color.Silver),
                new Person("Nick", new DateTime(1976, 2, 10), Color.Teal),
                new Person("Olive", new DateTime(1977, 3, 20), Color.White),
                new Person("Pendleton", new DateTime(1978, 4, 10), Color.Yellow),
                new Person("Queenie", new DateTime(1979, 5, 15), Color.Aqua),
                new Person("Rob", new DateTime(1980, 6, 30), Color.Blue),
#pragma warning disable 618
                new Person("Sally", new DateTime(1981, 7, 5), Color.Fuschia),
#pragma warning restore 618
                new Person("Timothy", new DateTime(1982, 8, 30), Color.Green),
                new Person("Uma", new DateTime(1983, 9, 10), Color.Lime),
                new Person("Victor", new DateTime(1984, 10, 20), Color.Maroon),
                new Person("Wendy", new DateTime(1985, 11, 5), Color.Navy),
                new Person("Xavier", new DateTime(1986, 12, 30), Color.Olive),
                new Person("Yvonne", new DateTime(1987, 1, 10), Color.Purple),
                new Person("Zachary", new DateTime(1988, 2, 5), Color.Red)
            };
			List<Person> people2 = new List<Person>
            {
                new Person("Abigail", new DateTime(1975, 1, 15), Color.Aqua),
                new Person("Bob", new DateTime(1976, 2, 20), Color.Black),
                new Person("Cathy", new DateTime(1977, 3, 10), Color.Blue),
#pragma warning disable 618
                new Person("David", new DateTime(1978, 4, 25), Color.Fuschia),
#pragma warning restore 618
                new Person("Eugenie", new DateTime(1979, 5, 5), Color.Gray),
                new Person("Freddie", new DateTime(1980, 6, 30), Color.Green),
                new Person("Greta", new DateTime(1981, 7, 15), Color.Lime),
                new Person("Harold", new DateTime(1982, 8, 10), Color.Maroon),
                new Person("Irene", new DateTime(1983, 9, 25), Color.Navy),
                new Person("Jonathan", new DateTime(1984, 10, 10), Color.Olive),
                new Person("Kathy", new DateTime(1985, 11, 20), Color.Purple),
                new Person("Larry", new DateTime(1986, 12, 5), Color.Red),
                new Person("Monica", new DateTime(1975, 1, 5), Color.Silver),
                new Person("Nick", new DateTime(1976, 2, 10), Color.Teal),
                new Person("Olive", new DateTime(1977, 3, 20), Color.White),
                new Person("Pendleton", new DateTime(1978, 4, 10), Color.Yellow),
                new Person("Queenie", new DateTime(1979, 5, 15), Color.Aqua),
                new Person("Rob", new DateTime(1980, 6, 30), Color.Blue),
#pragma warning disable 618
                new Person("Sally", new DateTime(1981, 7, 5), Color.Fuschia),
#pragma warning restore 618
                new Person("Timothy", new DateTime(1982, 8, 30), Color.Green),
                new Person("Uma", new DateTime(1983, 9, 10), Color.Lime),
                new Person("Victor", new DateTime(1984, 10, 20), Color.Maroon),
                new Person("Wendy", new DateTime(1985, 11, 5), Color.Navy),
                new Person("Xavier", new DateTime(1986, 12, 30), Color.Olive),
                new Person("Yvonne", new DateTime(1987, 1, 10), Color.Purple),
                new Person("Zachary", new DateTime(1988, 2, 5), Color.Red)
            };

            // Create the ListView.
            ListView listView = new ListView (ListViewCachingStrategy.RecycleElement)
            {
                // Source of data items.
                ItemsSource = new List<List<Person>> {people, people2},
				IsPullToRefreshEnabled = true,
				IsGroupingEnabled = true,

                // Define template for displaying each item.
                // (Argument of DataTemplate constructor is called for 
                //      each item; it must return a Cell derivative.)
                ItemTemplate = new MyDataTemplateSelector ()
            };

	        listView.Refreshing += async (sender, e) => {
		        await Task.Delay (5000);
		        listView.IsRefreshing = false;
	        };

	        listView.ItemSelected += (sender, args) => {
		        if (listView.SelectedItem == null)
			        return;
		        listView.SelectedItem = null;
	        };

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

            // Build the page.
            Content = new StackLayout
            {
                Children = 
                {
                    header,
                    listView
                }
            };
        }
    }
}