|
using System; |
|
using System.Collections; |
|
using System.Windows.Input; |
|
using Android.Content; |
|
using Android.Runtime; |
|
using Android.Support.V7.Widget; |
|
using Android.Util; |
|
using MvvmCross.Binding.Attributes; |
|
using MvvmCross.Binding.Droid.Views; |
|
|
|
namespace Droid.Lib.Ui |
|
{ |
|
[Register("MvxSpinner2")] |
|
public class MvxSpinner2 : AppCompatSpinner |
|
{ |
|
public MvxSpinner2(Context context, IAttributeSet attrs) |
|
: this(context, attrs, |
|
new MvxAdapter(context) |
|
{ |
|
SimpleViewLayoutId = global::Android.Resource.Layout.SimpleDropDownItem1Line |
|
}) |
|
{ |
|
} |
|
|
|
public MvxSpinner2(Context context, IAttributeSet attrs, IMvxAdapter adapter) |
|
: base(context, attrs) |
|
{ |
|
var itemTemplateId = MvxAttributeHelpers.ReadListItemTemplateId(context, attrs); |
|
var dropDownItemTemplateId = MvxAttributeHelpers.ReadDropDownListItemTemplateId(context, attrs); |
|
adapter.ItemTemplateId = itemTemplateId; |
|
adapter.DropDownItemTemplateId = dropDownItemTemplateId; |
|
this.Adapter = adapter; |
|
this.SetupHandleItemSelected(); |
|
} |
|
|
|
protected MvxSpinner2(IntPtr javaReference, JniHandleOwnership transfer) |
|
: base(javaReference, transfer) |
|
{ |
|
} |
|
|
|
public new IMvxAdapter Adapter |
|
{ |
|
get { return base.Adapter as IMvxAdapter; } |
|
set |
|
{ |
|
var existing = this.Adapter; |
|
if (existing == value) |
|
return; |
|
|
|
if (existing != null && value != null) |
|
{ |
|
value.ItemsSource = existing.ItemsSource; |
|
value.ItemTemplateId = existing.ItemTemplateId; |
|
} |
|
if (existing != null) |
|
{ |
|
existing.ItemsSource = null; |
|
} |
|
|
|
base.Adapter = value; |
|
} |
|
} |
|
|
|
[MvxSetToNullAfterBinding] |
|
public IEnumerable ItemsSource |
|
{ |
|
get { return this.Adapter.ItemsSource; } |
|
set { this.Adapter.ItemsSource = value; } |
|
} |
|
|
|
public int ItemTemplateId |
|
{ |
|
get { return this.Adapter.ItemTemplateId; } |
|
set { this.Adapter.ItemTemplateId = value; } |
|
} |
|
|
|
public int DropDownItemTemplateId |
|
{ |
|
get { return this.Adapter.DropDownItemTemplateId; } |
|
set { this.Adapter.DropDownItemTemplateId = value; } |
|
} |
|
|
|
public ICommand HandleItemSelected { get; set; } |
|
public ICommand HandleItemSelectedIndex { get; set; } |
|
|
|
private void SetupHandleItemSelected() |
|
{ |
|
base.ItemSelected += (sender, args) => |
|
{ |
|
var position = args.Position; |
|
this.HandleSelected(position); |
|
}; |
|
} |
|
|
|
protected virtual void HandleSelected(int position) |
|
{ |
|
if (HandleItemSelected != null) |
|
{ |
|
var item = Adapter.GetRawItem(position); |
|
if (HandleItemSelected.CanExecute(item)) |
|
HandleItemSelected.Execute(item); |
|
} |
|
|
|
if (HandleItemSelectedIndex != null && HandleItemSelectedIndex.CanExecute(position)) |
|
HandleItemSelectedIndex.Execute(position); |
|
} |
|
} |
|
} |