summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/Renderers/NavigationMenuRenderer.cs
blob: c7e45ad9f55df9ac5efc2f1b3dfa171ac02d1a99 (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
using System;
using System.ComponentModel;
using System.Linq;
using Android.Content;
using Android.Graphics;
using Android.Views;
using Android.Widget;
using AView = Android.Views.View;

namespace Xamarin.Forms.Platform.Android
{
	public sealed class NavigationMenuRenderer : ViewRenderer
	{
		public NavigationMenuRenderer()
		{
			AutoPackage = false;
		}

		GridView GridView
		{
			get { return Control as GridView; }
		}

		NavigationMenu NavigationMenu
		{
			get { return Element as NavigationMenu; }
		}

		protected override AView CreateNativeControl()
		{
			return new GridView(Context);
		}

		protected override void OnElementChanged(ElementChangedEventArgs<View> e)
		{
			base.OnElementChanged(e);

			if (e.OldElement == null)
			{
				var grid = (GridView)CreateNativeControl();
				grid.SetVerticalSpacing(20);

				SetNativeControl(grid);
			}

			UpdateTargets();
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged(sender, e);

			switch (e.PropertyName)
			{
				case "Targets":
					UpdateTargets();
					break;
			}
		}

		protected override void OnSizeChanged(int w, int h, int oldw, int oldh)
		{
			GridView.NumColumns = w > h ? 3 : 2;
			base.OnSizeChanged(w, h, oldw, oldh);
		}

		void UpdateTargets()
		{
			GridView.Adapter = new MenuAdapter(NavigationMenu);
		}

		class MenuElementView : LinearLayout
		{
			readonly ImageButton _image;
			readonly TextView _label;
			string _icon;

			public MenuElementView(Context context) : base(context)
			{
				Orientation = Orientation.Vertical;
				_image = new ImageButton(context);
				_image.SetScaleType(ImageView.ScaleType.FitCenter);
				_image.Click += (object sender, EventArgs e) =>
				{
					if (OnSelected != null)
						OnSelected();
				};
				AddView(_image, new LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent) { Gravity = GravityFlags.Center });

				_label = new TextView(context) { TextAlignment = global::Android.Views.TextAlignment.Center, Gravity = GravityFlags.Center };
				AddView(_label, new LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent));
			}

			public string Icon
			{
				get { return _icon; }
				set
				{
					_icon = value;
					Bitmap bitmap = Context.Resources.GetBitmap(_icon);
					_image.SetImageBitmap(bitmap);
				}
			}

			public string Name
			{
				get { return _label.Text; }
				set { _label.Text = value; }
			}

			public Action OnSelected { get; set; }
		}

		class MenuAdapter : BaseAdapter<Page>
		{
			readonly NavigationMenu _menu;

			public MenuAdapter(NavigationMenu menu)
			{
				_menu = menu;
			}

			#region implemented abstract members of BaseAdapter

			public override Page this[int index]
			{
				get { return _menu.Targets.ElementAtOrDefault(index); }
			}

			#endregion

			public override AView GetView(int position, AView convertView, ViewGroup parent)
			{
				MenuElementView menuItem = convertView as MenuElementView ?? new MenuElementView(parent.Context);
				Page item = this[position];
				menuItem.Icon = item.Icon;
				menuItem.Name = item.Title;
				menuItem.OnSelected = () => _menu.SendTargetSelected(item);
				return menuItem;
			}

			#region implemented abstract members of BaseAdapter

			public override long GetItemId(int position)
			{
				return 0;
			}

			public override int Count
			{
				get { return _menu.Targets.Count(); }
			}

			#endregion
		}
	}
}