【发布时间】:2014-11-05 15:51:30
【问题描述】:
在我的MvxGridView 项目上绑定Click 事件时遇到问题(我正在使用ItemClick 绑定)。
一切都很好。当我使用TextView 我的GoClick 方法被正确触发。但是当我在ItemTemplateView 中将TextView 更改为Button 时,将不再调用GoClick 方法。
根据this 的回答(选项编号 1),一切都应该可以正常工作。但在Button 的情况下则不然。
任何帮助表示赞赏,我被困在这里。
我的视图模型:
public class MyViewModel : MvxViewModel
{
private IEnumerable<MyListItem> items;
public MyViewModel()
{
this.items = new List<MyListItem>
{
new MyListItem {Name = "Item1"},
new MyListItem {Name = "Item2"},
};
}
public IEnumerable<MyListItem> Items
{
get { return this.items; }
}
public ICommand SelectItem
{
get { return new MvxCommand<MyListItem>(this.GoClick);}
}
public void GoClick(MyListItem item)
{
//doSomething
}
}
我的布局与MvxGridView:
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/black"
>
<Mvx.MvxGridView
android:layout_width="300dp"
android:layout_height="match_parent"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
local:MvxItemTemplate="@layout/itemtemplateview"
local:MvxBind="ItemsSource Items; ItemClick SelectItem"
/>
</LinearLayout>
我的ItemTemplateView(与TextView 配合得很好)
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
local:MvxBind="Text Name"
/>
<!-- CLICK ON THIS BUTTON DOES NOT WORK -->
<!--
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
local:MvxBind="Text Name"
/>
-->
</LinearLayout>
【问题讨论】:
-
类似,但我想知道为此创建自定义适配器真的有必要吗?我试图在 itemView.xml 中设置这些属性,但没有成功
-
我认为您可能只是面临一个 Android 问题 - 搜索像 stackoverflow.com/questions/6703390/… 这样的问答 - 如果您已经尝试过,请编辑问题以提供更多信息(cmets 很难了解)