我有一个绑定到自定义对象列表的自定义 MvxAdapter。我想在自定义适配器中以编程方式将我的对象绑定到我的 android 资源,而不是在 xml 中指定我的绑定。
我知道这是可能的,因为 Stuart 在这里回答了一个关于 SO 的问题,他说“......在自定义适配器中应用绑定并在你的活动中的 OnCreate 中设置该适配器”。
但是,我找不到这方面的任何示例,并且 CreateBindingSet() 在 GetView 或 GetBindableView 中不可用。
任何人都可以指出我正确的方向吗?
编辑:添加代码以说明我想在哪里手动进行绑定
protected override View GetBindableView(View convertView, object source, int templateId)
{
if (source is JobWithTabsViewModel.PictureFeature)
templateId = Resource.Layout.jobview_withtabs_features_item_picture;
else if (source is JobWithTabsViewModel.PointOfInterestFeature)
templateId = Resource.Layout.jobview_withtabs_features_item_poi;
return base.GetBindableView(convertView, source, templateId);
}
protected override View GetView(int position, View convertView, ViewGroup parent, int templateId)
{
var theView = base.GetView(position, convertView, parent, templateId);
var item = GetRawItem(position);
if (item is …Run Code Online (Sandbox Code Playgroud) 我创建了一个自定义控件(CustomCard),它是CardView控件的子类.我想在我的项目中在不同的地方使用这个控件.
例如,我可以手动将CustomCard放在xml布局中,或者我可能希望CustomCard成为MvxListView中的项目.关键是我希望尽可能多地重用代码,并从控制CustomCard类中获益.
在实例化CustomCard时,我使用标准布局inflater对其布局进行充气,请参阅代码:
using System;
using Android.Animation;
using Android.Content;
using Android.Support.V7.Widget;
using Android.Util;
using Android.Views;
using Android.Widget;
public class Card : CardView
{
private readonly Context _context;
public Card(Context context)
: base(context)
{
_context = context;
Init();
}
public Card(Context context, IAttributeSet attrs)
: base(context, attrs)
{
_context = context;
Init();
}
private void Init()
{
var inflater = (LayoutInflater) _context.GetSystemService(Context.LayoutInflaterService);
CardView = inflater.Inflate(Resource.Layout.base_card, this);
}
}
Run Code Online (Sandbox Code Playgroud)
在布局base_card.xml中,我有一些我想用MVVMCross绑定的元素,例如,
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:background="@android:color/white">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto" …Run Code Online (Sandbox Code Playgroud)