summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls/GalleryPages/ListPage.cs
blob: 5779c977f5146facce225e129af3029bc85767dd (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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Controls
{
	public class ListPage : ContentPage
	{
		ListScreen _listScreen;
		public ListPage ()
		{
			_listScreen = new ListScreen ();
			Content = new StackLayout {
				Children = {
					new Label {Text = "Foo"},
					_listScreen.View
				}
			};
		}
	}

	public class ListScreen
	{
		public ListView View { get; private set; }

		internal class A : INotifyPropertyChanged
		{
			string _text;
			public string Text {
				get {
					return _text;
				}
				set {
					_text = value;
					if(PropertyChanged != null)
						PropertyChanged(this, new PropertyChangedEventArgs("Text"));
				}
			}

			#region INotifyPropertyChanged implementation

			public event PropertyChangedEventHandler PropertyChanged;

			#endregion
		}

		[Preserve (AllMembers = true)]
		internal class ViewCellTest : ViewCell
		{
			static int s_inc = 0;

			public ViewCellTest ()
			{
				var stackLayout = new StackLayout {
					Orientation = StackOrientation.Horizontal
				};

				var label = new Label ();
				label.SetBinding (Label.TextProperty, "Text");

				var box = new BoxView {WidthRequest = 100, HeightRequest = 10, Color = Color.Red};

				stackLayout.Children.Add (label);
				stackLayout.Children.Add (box);

				View = stackLayout;
			}

			protected override void OnAppearing ()
			{
				base.OnAppearing ();
				Debug.WriteLine ("Appearing: " + (BindingContext as A)?.Text + " : " + s_inc);
				s_inc++;
			}

			protected override void OnDisappearing ()
			{
				base.OnDisappearing ();
				Debug.WriteLine ("Disappearing: " + (BindingContext as A)?.Text + " : " + s_inc);
				s_inc++;
			}
		}

		public ListScreen ()
		{

			View = new ListView (ListViewCachingStrategy.RecycleElement);

			View.RowHeight = 30;

			var n = 500;
			var items = Enumerable.Range (0, n).Select (i => new A {Text = i.ToString ()}).ToList ();
			View.ItemsSource = items;

			View.ItemTemplate = new DataTemplate (typeof (ViewCellTest));

			View.ItemSelected += (sender, e) => {
				var cell = (e.SelectedItem as A);
				if (cell == null)
					return;
				var x = int.Parse (cell.Text);
				if (x == 5) {
					n += 10;
					View.ItemsSource = Enumerable.Range (0, n).Select (i => new A { Text = i.ToString () }).ToList ();
				} else {
					cell.Text = (x + 1).ToString ();
				}
			};

				
		}
	}
}