Skip to content

Instantly share code, notes, and snippets.

@softlion
Last active May 13, 2021 18:59
Show Gist options
  • Save softlion/8271623 to your computer and use it in GitHub Desktop.
Save softlion/8271623 to your computer and use it in GitHub Desktop.
mvvmcross : Bind a static RadioGroup using zero based indexes
/// <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);
}
}
<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>
@dukeofharen
Copy link

Thanks, works like a charm!

@ukod
Copy link

ukod commented Nov 28, 2017

It's exactly that I need, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment