我认为执行此操作的简单方法是在列表项中使用自定义“视图”。
Note:这里的“视图”指的是 Android 视图 - 不是模型-视图-视图模型视图 - 抱歉命名混乱!
创建自定义视图很容易......
只需创建一个自定义视图 - 例如
namespace Angevelle.App1.UI.Droid.Controls
{
public class MyText : EditText
{
public MyText(Context context, IAttributeSet attrs)
: base(context, attrs)
{
this.EditorAction += OnEditorAction;
}
private void OnEditorAction(object sender, EditorActionEventArgs editorActionEventArgs)
{
if (editorActionEventArgs.ActionId == ImeAction.Done)
{
// this code not tested - but something like this should work
var imm = (InputMethodManager)Context.GetSystemService(Context.InputMethodService);
imm.HideSoftInputFromWindow(WindowToken, 0);
}
}
}
}
然后您可以在 AXML 中使用该视图,就像使用 Android 或 Mvx 视图一样:
<angevelle.app1.ui.droid.controls.MyText
android:layout_height=....
/>
如果您正在寻找angevelle.app1.ui.droid.controls
太冗长了,那么您可以在 setup.cs 中使用缩写来缩短它:
protected override IDictionary<string, string> ViewNamespaceAbbreviations
{
get
{
var abbreviations = base.ViewNamespaceAbbreviations;
abbreviations["Abv"] = "angevelle.app1.ui.droid.controls";
return abbreviations;
}
}
那么你可以使用:
<Abv.MyText
android:layout_height=....
/>
另一种方法可能是以某种方式自定义列表......
如果您确实需要完全自定义列表视图及其适配器,那么可以使用相同类型的技术轻松完成 - 从 UI 项目中的 MvxBindableListView 继承:
public class MyListView : MvxBindableListView
{
public MyListView(Context context, IAttributeSet attrs);
: base(context, attrs, new MyListAdapter(context))
{
}
}
其中 MyListAdapter 覆盖视图创建:
public class MyListAdapter : MvxBindableListAdapter
{
public MyListAdapter(Context context)
: base(context)
{
}
// put your custom Adapter code here... e.g.:
protected override MvxBindableListItemView CreateBindableView(object source, int templateId)
{
return new MySpecialListItemView(_context, _bindingActivity, templateId, source);
}
}
其中 MySpecialListItemView 继承自 MvxBindableListItemView 但添加了您自己的自定义功能。
使用这种方法,您的列表将从:
<Mvx.MvxBindableListView
....
/>
to:
<Abv.MyListView
....
/>
有关自定义视图的更多示例,请查看 GitHub - 例如在一些日历、ColorPicker、ActionBar 项目中https://github.com/Cheesebaron
不要指望您的自定义控件能够在 xamarin 设计器中呈现(好吧,还没有......)
最后两个注意事项...
要重用代码......你可能想把它HideSoftInputFromWindow
扩展方法中的功能以某种方式这样你就可以调用anyEditText.HideOnDone()
在 Views/UIViews 上使用 Monodroid/monotouch 事件时要小心 - 这些事件往往使用本机委托/侦听器 - 因此有时您会发现附加某些内容来订阅一个事件可能会取消附加其他内容!一般来说,只要不将 C# 事件订阅与本机侦听器/委托处理程序同时混合和匹配,就可以了。