public
Created

  • Download Gist
MvxGridView.cs
C#
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
// 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);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.