Created
May 4, 2013 18:23
-
-
Save slodge/5518288 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
// MvxGridView.cs | |
// (c) Copyright Cirrious Ltd. http://www.cirrious.com | |
// MvvmCross is licensed using Microsoft Public License (Ms-PL) | |
// Contributions and inspirations noted in readme.md and license.txt | |
// | |
// Project Lead - Stuart Lodge, @slodge, me@slodge.com | |
using System.Collections; | |
using System.Windows.Input; | |
using Android.Content; | |
using Android.Util; | |
using Android.Widget; | |
using Cirrious.MvvmCross.Binding.Attributes; | |
namespace Cirrious.MvvmCross.Binding.Droid.Views | |
{ | |
public class MvxGridView | |
: GridView | |
{ | |
public MvxGridView(Context context, IAttributeSet attrs) | |
: this(context, attrs, new MvxAdapter(context)) | |
{ | |
} | |
public MvxGridView(Context context, IAttributeSet attrs, MvxAdapter adapter) | |
: base(context, attrs) | |
{ | |
var itemTemplateId = MvxAttributeHelpers.ReadListItemTemplateId(context, attrs); | |
adapter.ItemTemplateId = itemTemplateId; | |
Adapter = adapter; | |
SetupItemClickListeners(); | |
} | |
public new MvxAdapter Adapter | |
{ | |
get { return base.Adapter as MvxAdapter; } | |
set | |
{ | |
var existing = Adapter; | |
if (existing == value) | |
return; | |
if (existing != null && value != null) | |
{ | |
value.ItemsSource = existing.ItemsSource; | |
value.ItemTemplateId = existing.ItemTemplateId; | |
} | |
base.Adapter = value; | |
} | |
} | |
[MvxSetToNullAfterBinding] | |
public IEnumerable ItemsSource | |
{ | |
get { return Adapter.ItemsSource; } | |
set { Adapter.ItemsSource = value; } | |
} | |
public int ItemTemplateId | |
{ | |
get { return Adapter.ItemTemplateId; } | |
set { Adapter.ItemTemplateId = value; } | |
} | |
public new ICommand ItemClick { get; set; } | |
public new ICommand ItemLongClick { get; set; } | |
protected void SetupItemClickListeners() | |
{ | |
base.ItemClick += (sender, args) => ExecuteCommandOnItem(this.ItemClick, args.Position); | |
base.ItemLongClick += (sender, args) => ExecuteCommandOnItem(this.ItemLongClick, args.Position); | |
} | |
protected virtual void ExecuteCommandOnItem(ICommand command, int position) | |
{ | |
if (command == null) | |
return; | |
var item = Adapter.GetRawItem(position); | |
if (item == null) | |
return; | |
if (!command.CanExecute(item)) | |
return; | |
command.Execute(item); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment