/
SegmentedControlImplementation.cs
237 lines (189 loc) · 7.39 KB
/
SegmentedControlImplementation.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
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
using System;
using Xamarin.Forms;
using SegmentedControl.FormsPlugin.Android;
using Xamarin.Forms.Platform.Android;
using Android.Widget;
using Android.Views;
using Android.Graphics.Drawables;
using Android.Content;
[assembly: ExportRenderer(typeof(SegmentedControl.FormsPlugin.Abstractions.SegmentedControl), typeof(SegmentedControlRenderer))]
namespace SegmentedControl.FormsPlugin.Android
{
/// <summary>
/// SegmentedControl Renderer
/// </summary>
public class SegmentedControlRenderer : ViewRenderer<Abstractions.SegmentedControl, RadioGroup>
{
RadioGroup nativeControl;
RadioButton _rb;
Context context;
public SegmentedControlRenderer(Context context) : base(context)
{
this.context = context;
}
protected override void OnElementChanged(ElementChangedEventArgs<Abstractions.SegmentedControl> e)
{
base.OnElementChanged(e);
if (Control == null)
{
// Instantiate the native control and assign it to the Control property with
// the SetNativeControl method
var layoutInflater = LayoutInflater.From(context);
var view = layoutInflater.Inflate(Resource.Layout.RadioGroup, null);
nativeControl = (RadioGroup)layoutInflater.Inflate(Resource.Layout.RadioGroup, null);
for (var i = 0; i < Element.Children.Count; i++)
{
var o = Element.Children[i];
var rb = (RadioButton)layoutInflater.Inflate(Resource.Layout.RadioButton, null);
rb.LayoutParameters = new RadioGroup.LayoutParams(0, LayoutParams.WrapContent, 1f);
rb.Text = o.Text;
if (i == 0)
rb.SetBackgroundResource(Resource.Drawable.segmented_control_first_background);
else if (i == Element.Children.Count - 1)
rb.SetBackgroundResource(Resource.Drawable.segmented_control_last_background);
ConfigureRadioButton(i, rb);
nativeControl.AddView(rb);
}
var option = (RadioButton)nativeControl.GetChildAt(Element.SelectedSegment);
if (option != null)
option.Checked = true;
SetNativeControl(nativeControl);
}
if (e.OldElement != null)
{
// Unsubscribe from event handlers and cleanup any resources
if (nativeControl != null)
nativeControl.CheckedChange -= NativeControl_ValueChanged;
}
if (e.NewElement != null)
{
// Configure the control and subscribe to event handlers
nativeControl.CheckedChange += NativeControl_ValueChanged;
}
}
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (nativeControl == null || Element == null) return;
switch (e.PropertyName)
{
case "Renderer":
Element?.SendValueChanged();
break;
case "SelectedSegment":
var option = (RadioButton)nativeControl.GetChildAt(Element.SelectedSegment);
if (option != null)
option.Checked = true;
if (Element.SelectedSegment < 0)
{
var layoutInflater = LayoutInflater.From(context);
nativeControl = (RadioGroup)layoutInflater.Inflate(Resource.Layout.RadioGroup, null);
for (var i = 0; i < Element.Children.Count; i++)
{
var o = Element.Children[i];
var rb = (RadioButton)layoutInflater.Inflate(Resource.Layout.RadioButton, null);
rb.LayoutParameters = new RadioGroup.LayoutParams(0, LayoutParams.WrapContent, 1f);
rb.Text = o.Text;
if (i == 0)
rb.SetBackgroundResource(Resource.Drawable.segmented_control_first_background);
else if (i == Element.Children.Count - 1)
rb.SetBackgroundResource(Resource.Drawable.segmented_control_last_background);
ConfigureRadioButton(i, rb);
nativeControl.AddView(rb);
}
nativeControl.CheckedChange += NativeControl_ValueChanged;
SetNativeControl(nativeControl);
}
Element.SendValueChanged();
break;
case "TintColor":
OnPropertyChanged();
break;
case "IsEnabled":
OnPropertyChanged();
break;
case "SelectedTextColor":
var v = (RadioButton)nativeControl.GetChildAt(Element.SelectedSegment);
v.SetTextColor(Element.SelectedTextColor.ToAndroid());
break;
}
}
void OnPropertyChanged()
{
if (nativeControl != null && Element != null)
{
for (var i = 0; i < Element.Children.Count; i++)
{
var rb = (RadioButton)nativeControl.GetChildAt(i);
ConfigureRadioButton(i, rb);
}
}
}
void ConfigureRadioButton(int i, RadioButton rb)
{
if (i == Element.SelectedSegment)
{
rb.SetTextColor(Element.SelectedTextColor.ToAndroid());
_rb = rb;
}
else
{
var textColor = Element.IsEnabled ? Element.TintColor.ToAndroid() : Element.DisabledColor.ToAndroid();
rb.SetTextColor(textColor);
}
GradientDrawable selectedShape;
GradientDrawable unselectedShape;
var gradientDrawable = (StateListDrawable)rb.Background;
var drawableContainerState = (DrawableContainer.DrawableContainerState)gradientDrawable.GetConstantState();
var children = drawableContainerState.GetChildren();
// Doesnt works on API < 18
selectedShape = children[0] is GradientDrawable? (GradientDrawable)children[0] : (GradientDrawable)((InsetDrawable)children[0]).Drawable;
unselectedShape = children[1] is GradientDrawable? (GradientDrawable)children[1] : (GradientDrawable)((InsetDrawable)children[1]).Drawable;
var color = Element.IsEnabled ? Element.TintColor.ToAndroid() : Element.DisabledColor.ToAndroid();
selectedShape.SetStroke(3, color);
selectedShape.SetColor(color);
unselectedShape.SetStroke(3, color);
rb.Enabled = Element.IsEnabled;
}
void NativeControl_ValueChanged(object sender, RadioGroup.CheckedChangeEventArgs e)
{
var rg = (RadioGroup)sender;
if (rg.CheckedRadioButtonId != -1)
{
var id = rg.CheckedRadioButtonId;
var radioButton = rg.FindViewById(id);
var radioId = rg.IndexOfChild(radioButton);
var rb = (RadioButton)rg.GetChildAt(radioId);
var color = Element.IsEnabled ? Element.TintColor.ToAndroid() : Element.DisabledColor.ToAndroid();
_rb?.SetTextColor(color);
rb.SetTextColor(Element.SelectedTextColor.ToAndroid());
_rb = rb;
Element.SelectedSegment = radioId;
}
}
protected override void Dispose(bool disposing)
{
if (nativeControl != null)
{
nativeControl.CheckedChange -= NativeControl_ValueChanged;
nativeControl.Dispose();
nativeControl = null;
_rb = null;
}
try
{
base.Dispose(disposing);
}
catch (Exception ex)
{
return;
}
}
/// <summary>
/// Used for registration with dependency service
/// </summary>
public static void Init() {
var temp = DateTime.Now;
}
}
}