Forked from softlion/MvxRadioGroupSelectedIndexBinding.cs
Created
March 27, 2014 03:26
-
-
Save dbeattie71/9799504 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Author: softlion (softlion@softlion.com) | |
/// </summary> | |
/// <remarks> | |
/// Thanks to: Stuart Lodge | |
/// </remarks> | |
public class MvxRadioGroupSelectedIndexBinding : MvxAndroidTargetBinding | |
{ | |
bool stopListeningCheckChanged = false; | |
private int selectedIndex = -2; | |
public int SelectedIndex | |
{ | |
get { return selectedIndex; } | |
set { if(value != selectedIndex) { selectedIndex = value; FireValueChanged(SelectedIndex); } } | |
} | |
public static void Register(IMvxTargetBindingFactoryRegistry registry) | |
{ | |
registry.RegisterCustomBindingFactory<RadioGroup>("SelectedIndex", radioGroup => new MvxRadioGroupSelectedIndexBinding(radioGroup)); | |
} | |
public MvxRadioGroupSelectedIndexBinding(RadioGroup radioGroup) : base(radioGroup) | |
{ | |
if (radioGroup == null) | |
{ | |
Mvx.Trace(MvxTraceLevel.Error, "RadioGroup SelectedIndex: radioGroup is null"); | |
return; | |
} | |
radioGroup.CheckedChange += CheckedChange; | |
radioGroup.ChildViewAdded += RadioGroupOnChildViewAdded; | |
//radioGroup.ChildViewRemoved += RadioGroupOnChildViewRemoved; | |
} | |
private void RadioGroupOnChildViewAdded(object sender, ViewGroup.ChildViewAddedEventArgs childViewAddedEventArgs) | |
{ | |
var radioGroup = Target as RadioGroup; | |
if (selectedIndex == radioGroup.ChildCount-1) | |
{ | |
stopListeningCheckChanged = true; | |
radioGroup.Check(radioGroup.GetChildAt(selectedIndex).Id); | |
stopListeningCheckChanged = false; | |
} | |
} | |
//private void RadioGroupOnChildViewRemoved(object sender, ViewGroup.ChildViewRemovedEventArgs childViewRemovedEventArgs) | |
//{ | |
// ChildViewCountChanged(); | |
//} | |
private void CheckedChange(object sender, RadioGroup.CheckedChangeEventArgs e) | |
{ | |
if (stopListeningCheckChanged) | |
return; | |
var radioGroup = Target as RadioGroup; | |
var checkedId = e.CheckedId; | |
if (checkedId == View.NoId) | |
{ | |
SelectedIndex = -1; | |
return; | |
} | |
for (var i = radioGroup.ChildCount - 1; i >= 0; i--) | |
{ | |
if (checkedId == radioGroup.GetChildAt(i).Id) | |
{ | |
SelectedIndex = i; | |
return; | |
} | |
} | |
SelectedIndex = -1; | |
Mvx.Trace(MvxTraceLevel.Error, "RadioGroup id not found: {0}", checkedId); | |
} | |
public override void SetValue(object index) | |
{ | |
var radioGroup = Target as RadioGroup; | |
if (radioGroup == null) | |
return; | |
stopListeningCheckChanged = true; | |
selectedIndex = (int)index; | |
if (selectedIndex < 0 || selectedIndex >= radioGroup.ChildCount) | |
{ | |
radioGroup.ClearCheck(); | |
//selectedIndex = -1; | |
} | |
else | |
{ | |
radioGroup.Check(radioGroup.GetChildAt(selectedIndex).Id); | |
//selectedIndex = newIndex; | |
} | |
stopListeningCheckChanged = false; | |
} | |
public override Type TargetType | |
{ | |
get { return typeof(object); } | |
} | |
protected override void SetValueImpl(object target, object value) | |
{ | |
} | |
public override MvxBindingMode DefaultMode | |
{ | |
get { return MvxBindingMode.TwoWay; } | |
} | |
protected override void Dispose(bool isDisposing) | |
{ | |
if (isDisposing) | |
{ | |
var radioGroup = Target as RadioGroup; | |
if (radioGroup != null) | |
{ | |
radioGroup.CheckedChange -= CheckedChange; | |
radioGroup.ChildViewAdded -= RadioGroupOnChildViewAdded; | |
//radioGroup.ChildViewRemoved -= ChildViewCountChanged; | |
} | |
} | |
base.Dispose(isDisposing); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RadioGroup | |
android:orientation="horizontal" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
local:MvxBind="SelectedIndex YourIntegerProperty"> | |
<RadioButton | |
android:text="first" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
<RadioButton | |
android:text="last" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" /> | |
</RadioGroup> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment