summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls/GalleryPages/UnevenListGallery.cs
blob: 1c8b16c41c49bfcceb977c24349dbcbb95aa3ba4 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xamarin.Forms.Controls
{
	public class UnevenListGallery : ContentPage
	{
		public UnevenListGallery ()
		{
			Padding = new Thickness (0, 20, 0, 0);

			var list = new ListView {
				HasUnevenRows = true
			};

			bool next = true;
			list.ItemTemplate = new DataTemplate (() => {
				bool tall = next;
				next = !next;
				
				var cell = new TextCell {
					Height = (tall) ? 88 : 44
				};

				cell.SetBinding (TextCell.TextProperty, ".");
				return cell;
			});

			list.ItemsSource = new[] { "Tall", "Short", "Tall", "Short" };

			var listViewCellDynamicHeight = new ListView {
				HasUnevenRows = true,
				AutomationId= "unevenCellListGalleryDynamic"
			};

			listViewCellDynamicHeight.ItemsSource = new [] { 
				@"That Flesh is heir to? 'Tis a consummation
Devoutly to be wished. To die, to sleep,
To sleep, perchance to Dream; Aye, there's the rub,
For in that sleep of death, what dreams may come,That Flesh is heir to? 'Tis a consummation
Devoutly to be wished. To die, to sleep,
To sleep, perchance to Dream; Aye, there's the rub,
For in that sleep of death, what dreams may come",
			};

			listViewCellDynamicHeight.ItemTemplate = new DataTemplate (typeof(UnevenRowsCell));

			listViewCellDynamicHeight.ItemTapped += (sender, e) => {
				if (e == null)
					return; // has been set to null, do not 'process' tapped event
				((ListView)sender).SelectedItem = null; // de-select the row
			};

			var grd = new Grid ();

			grd.RowDefinitions.Add (new RowDefinition ());
			grd.RowDefinitions.Add (new RowDefinition ());
		
			grd.Children.Add (listViewCellDynamicHeight);
			grd.Children.Add (list);
		
			Grid.SetRow (list, 1);
		
			Content =  grd;
		}

		public class UnevenRowsCell : ViewCell
		{
			public UnevenRowsCell ()
			{
				var label1 = new Label {
					Text = "Label 1",
					FontSize = Device.GetNamedSize (NamedSize.Small, typeof(Label))
				};
				label1.SetBinding (Label.TextProperty, new Binding ("."));

				View = new StackLayout {
					Orientation = StackOrientation.Vertical,
					VerticalOptions = LayoutOptions.StartAndExpand,
					Padding = new Thickness (15, 5, 5, 5),
					Children = { label1 }
				};
			}
			// This is the code used before and still works by setting the height of the cell
			//		const int avgCharsInRow = 35;
			//		const int defaultHeight = 44;
			//		const int extraLineHeight = 20;
			//		protected override void OnBindingContextChanged ()
			//		{
			//			base.OnBindingContextChanged ();
			//
			//			if (Device.OS == TargetPlatform.iOS) {
			//				var text = (string)BindingContext;
			//
			//				var len = text.Length;
			//
			//				if (len < (avgCharsInRow * 2)) {
			//					// fits in one cell
			//					Height = defaultHeight;
			//				} else {
			//					len = len - (avgCharsInRow * 2);
			//					var extraRows = len / 35;
			//					Height = defaultHeight + extraRows * extraLineHeight;
			//				}
			//			}
			//		}
		}
	}
}