我在我的MvvmCross PCL中有一个viewModel,它包含一个MyClass的列表。 并且这个MyClass包含一个int类型的成员,称为ResponseId。 我想创建一个listView的自定义布局,其中每个listItem包含不同的控件(例如,ratingBar,RadioButton等)的ResponseId取决于该ListItem的ResponseId的值。 如何设计listItem的布局?MvvmCross中的listView自定义布局
[Activity(Label = "View for MetricsByCat2ViewModel")]
public class MetricsByCat2View : MvxActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.MetricsByCat2View);
}
protected override void OnViewModelSet()
{
SetContentView(Resource.Layout.MetricsByCat2View);
var list = FindViewById<MvxListView>(Resource.Id.TheListView);
TextView tv = FindViewById<TextView>(Resource.Id.tv);
try
{
list.Adapter = new CustomAdapter(this as IMvxAndroidBindingContext,
BindingContext as IMvxAndroidBindingContext);
}
catch (Exception e)
{
tv.Text = e.Message;
}
}
public class CustomAdapter : MvxAdapter
{
public CustomAdapter(IMvxAndroidBindingContext context,
IMvxAndroidBindingContext bindingContext)
: base((Android.Content.Context)context, bindingContext)
{
}
public override int GetItemViewType(int position)
{
var item = GetRawItem(position);
if (item is StarRating)
return 1;
return 0;
}
public override int ViewTypeCount
{
get { return 2; }
}
protected override View GetBindableView(View convertView, object source,
int templateId)
{
if (source is StarRating)
templateId = Resource.Layout.item_met2;
// else if (source is Dog)
// templateId = Resource.Layout.ListItem_Dog;
return base.GetBindableView(convertView, source, templateId);
}
}
}
我指的是你告诉@Stuart先生的同样的例子。 https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/Working%20With%20Collections – Suraj