返回ImageView的例子,当我在GridView的项目点击 [英] Return ImageView instance when i click on the item GridView
问题描述
如何返回ImageView的实例时,我对项目的GridView点击?
How to return ImageView instance when i click on the item GridView?
创建自定义绑定事件项目单击:
public class ItemClickSquareBinding
: MvxBaseAndroidTargetBinding
{
private readonly GridView _gridView;
private IMvxCommand _command;
public ItemClickSquareBinding(GridView gridView)
{
_gridView = gridView;
_gridView.ItemClick += GridView_ItemClick;
}
private void GridView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
//----->Get ImageView
}
public override void SetValue(object value)
{
_command = (IMvxCommand)value;
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
{
_gridView.ItemClick -= GridView_ItemClick;
}
base.Dispose(isDisposing);
}
public override Type TargetType
{
get { return typeof(IMvxCommand); }
}
public override MvxBindingMode DefaultMode
{
get { return MvxBindingMode.OneWay; }
}
}
我的GridView的:
<cirrious.mvvmcross.binding.android.views.MvxBindableGridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="4"
android:gravity="center"
android:listSelector="#00000000"
local:MvxItemTemplate="@layout/itemimage"
local:MvxBind="{'ItemsSource':{'Path':'Squares'}, 'ClickItemSquare':{'Path':'ClickCommand'}}" />
我的ImageView的:
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res/LeSommet.ZooSnap.UI.Android"
android:layout_width="80dp"
android:layout_height="80dp"
android:padding="5dp"
android:layout_gravity="center"
local:MvxBind="{'ResourcesImagePath':{'Path':'ImagePath'}}"
/>
如何返回ImageView的实例时,我对项目的GridView点击? (或者我怎么可能会返回点击它我对象的实例)
推荐答案
以下三个答案......我preFER一个标记 3
Three answers below ... I prefer the one marked 3
在一个真正的基本层面,我想你可以只使用内置的此绑定。
At a really basic level, I think you can just use the built-in binding for this.
看着项目单击
在<一个href=\"https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.Binding.Droid/Views/MvxBindableGridView.cs\" rel=\"nofollow\">https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.Binding.Droid/Views/MvxBindableGridView.cs你应该能够只是做:
Looking at ItemClick
in https://github.com/slodge/MvvmCross/blob/vnext/Cirrious/Cirrious.MvvmCross.Binding.Droid/Views/MvxBindableGridView.cs you should be able to just do:
<cirrious.mvvmcross.binding.android.views.MvxBindableGridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="4"
android:gravity="center"
android:listSelector="#00000000"
local:MvxItemTemplate="@layout/itemimage"
local:MvxBind="{'ItemsSource':{'Path':'Squares'}, 'ItemClick':{'Path':'ClickCommand'}}" />
其中,点击指令
是这样的:
public ICommand ClickCommand { get { return new MvxRelayCommand<Square>(square => square.Foo()); } }
如果这不起作用,那么请筹集与测试code <错误/一> - 格被社会各界捐赠给MvvmCross所以它不是我还没用过
If this doesn't work, then please raise a bug with test code - the grid was community donated to MvvmCross so it's not something I've used yet.
如果你想做的自定义绑定。
If you do want to do the custom binding.
使用IntelliSense,它看起来像一个 AdapterView.ItemClickEventArgs
有几个可能有用的属性:
Using Intellisense, it looks like an AdapterView.ItemClickEventArgs
has a couple of potentially useful properties:
e.View;
e.Position;
所以,你可以访问查看
如果你想 - 然后可以使用的东西像 FindViewById&LT;&GT;
找到包含意见。
So you can access the View
if you want to - and can then use things like FindViewById<>
to find contained views.
或者你也可以访问位置
,然后可以:
- 使用适配器方法,如公众覆盖对象的GetItem(INT位置)
来获得一个Java包装的对象从原来的数组
- 或者可以使用访问方法直接传入阵列或枚举上
Or you can access the Position
and can then:
- use Adapter methods like public override Object GetItem(int position)
to get a Java wrapped object from your original array
- or can use accessor method direct on your incoming array or enumerable.
我一般不做这种习俗的结合,当所有我想要做的就是点击一个列表项列表项或一部分。
I generally don't do this sort of custom binding when all I want to do is click on a list item or part of a list item.
相反,我写我的ViewModels,使他们暴露启用行为的集合 - 我尝试present Model对象的激活行为包装而不是模型对象本身
Instead I write my ViewModels so that they expose behaviour-enabled collections - I try to present a behaviour-enabled wrapper of the Model object rather than the Model object itself.
在这种方法中,视图模型暴露了一个列表&LT;的
代替列表&LT; WrappedSquare&GT广场&GT;
In this approach the ViewModel exposes a List<WrappedSquare>
instead of a List<Square>
public WrappedSquare
{
Square _saure;
SquareViewModel _square;
public WrappedSquare(Square square, SquareViewModel parent)
{
/* assignment */
}
public ICommand TheCommand { get { return MvxRelayCommand(() -> _parent.DoStuff(_square)); } }
public Square TheSquare { get { return _square; } }
}
列表项中的AXML再有类似的绑定:
The axml within the list item then has bindings like:
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res/LeSommet.ZooSnap.UI.Android"
android:layout_width="80dp"
android:layout_height="80dp"
android:padding="5dp"
android:layout_gravity="center"
local:MvxBind="{'ResourcesImagePath':{'Path':'TheSquare.ImagePath'},'Click':{'Path':'TheCommand'}}"
/>
和网格的AXML是:
<cirrious.mvvmcross.binding.android.views.MvxBindableGridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="4"
android:gravity="center"
android:listSelector="#00000000"
local:MvxItemTemplate="@layout/itemimage"
local:MvxBind="{'ItemsSource':{'Path':'WrappedSquares'}}" />
这个例子code从<一个适应href=\"http://stackoverflow.com/questions/12682082/mvvmcross-changing-viewmodel-within-a-mvxbindablelistview\">MVVMCross一个MvxBindableListView 内改变视图模型 - 答案有有关这个问题太
This example code is adapted from MVVMCross changing ViewModel within a MvxBindableListView - the answer there is relevant to this question too.
这种方法的一个实际例子是使用会议样本 WithCommand&LT; T&GT;
来实现从名单详细信息视图导航
One real example of this approach is the Conference sample which uses WithCommand<T>
to enable navigation from lists to detail views.
不过...有关于这种方法的一个警告 - 请注意,使用 WithCommand&LT时,T&GT;
,我们发现在iOS中/ MonoTouch的内存泄漏 - 基本上是垃圾回收拒绝收集嵌入 MvxRelayCommand
- 这就是为什么 WithCommand&LT; T&GT;
是的IDisposable
为什么 BaseSessionListViewMode
清除列表并配置 WithCommand
元素时,视图分离。
However... there is one warning on this approach - please note that when using WithCommand<T>
we discovered a memory leak in iOS/MonoTouch - basically the GarbageCollection refused to collect the embedded MvxRelayCommand
- which is why WithCommand<T>
is IDisposable
and why BaseSessionListViewMode
clears the list and disposes the WithCommand
elements when views are detached.
这篇关于返回ImageView的例子,当我在GridView的项目点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!