在我的项目中,我想创建一个MvxRecyclerView来显示用户在特定日期购买的所有产品。所以我有一个主要的MvxRecyclerView布局和一个扩展MvxRecyclerAdapter的Adapter类。在这个MvxRecyclerAdapter中,我创建一个包含的布局MvxRecyclerView根据日期填充所有项目。
当我尝试创建适配器时,Mvvmcross没有显示子适配器数据,因为子适配器的OnCreateViewHolder正在抛出错误。
请检查下面我的父适配器类:
public class MyRecyler1Adapter: MvxRecyclerAdapter
{
private Activity _activity;
public static int MEETING_TYPE = 0;
public static int DOCUMENTS_TYPE = 1;
public NewResponse _response;
Meeting1Adapter adapter;
Documents1Adapter docadapter;
public List<MeetingDetails> meetingDetailList { get; set; }
public List<DocumentDetails> docDetailsList { get; set; }
public bool btnDocStatus = false;
public NewViewModel viewModel;
public MyRecyler1Adapter(IMvxAndroidBindingContext bindingContext, Activity context, NewResponse response,NewViewModel model)
: base(bindingContext)
{
this._activity = context;
this._response = response;
this.viewModel = model;
}
public override int ItemCount
{
get { return 2; }
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
if (position == 0 && _response.Meetings != null)
{
var meeting = holder as MeetingsTypeViewHolder;
meeting.txtMeg.Text = "Meeting";_response.Meetings.Meetings[position].MeetingTitle+position);
adapter = new Meeting1Adapter(BindingContext, _activity, _response.Meetings);
meeting.recycle.SetAdapter(adapter);
meetingDetailList = _response.Meetings.Meetings;
}
else if (position == 1 && _response.Documents != null)
{
var documents = holder as DocumentsTypeViewHolder;
documents.txtMsg.Text = "Documents";
docadapter = new Documents1Adapter((IMvxAndroidBindingContext)BindingContext,_activity, _response.Documents);
documents.recycle.SetAdapter(docadapter);
docDetailsList = _response.Documents.Documents;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
View itemView;
switch (viewType)
{
case 0:
if (_response.Meetings != null)
{
var itemBindingContext = new MvxAndroidBindingContext(parent.Context, this.BindingContext.LayoutInflaterHolder);
var id = Resource.Layout.layout1;
itemView = LayoutInflater.From(parent.Context).Inflate(id, parent, false);
return new MeetingsTypeViewHolder(itemView, itemBindingContext);
}
case 1:
if (_response.Documents != null)
{
var itemBindingContext = new MvxAndroidBindingContext(parent.Context, this.BindingContext.LayoutInflaterHolder);
var id1 = Resource.Layout.layout2;
itemView = LayoutInflater.From(parent.Context).Inflate(id1, parent, false);
return new DocumentsTypeViewHolder(itemView, itemBindingContext);
}
}
return null;
}
public class MeetingsTypeViewHolder : MvxRecyclerViewHolder
{
public TextView txtMeg { get; private set; }
public MvxRecyclerView recycle { get; set; }
public Button btnShowMore { get; set; }
public MeetingsTypeViewHolder(View itemView, IMvxAndroidBindingContext context) : base(itemView, context)
{
txtMeg = itemView.FindViewById<TextView>(Resource.Id.textView1);
recycle = itemView.FindViewById<MvxRecyclerView>(Resource.Id.rec1);
btnShowMore = itemView.FindViewById<Button>(Resource.Id.btnShowMore);
}
}
public class DocumentsTypeViewHolder : MvxRecyclerViewHolder
{
public TextView txtMsg { get; private set; }
public RecyclerView recycle { get; set; }
public Button btnShowMore { get; set; }
public DocumentsTypeViewHolder(View itemView, IMvxAndroidBindingContext context) : base(itemView, context)
{
txtMsg = itemView.FindViewById<TextView>(Resource.Id.textView1);
recycle = itemView.FindViewById<RecyclerView>(Resource.Id.rec1);
btnShowMore = itemView.FindViewById<Button>(Resource.Id.btnShowMore);
}
}
public override int GetItemViewType(int position)
{
switch (position)
{
case 0:
return MEETING_TYPE;
case 1:
return DOCUMENTS_TYPE;
default:
return -1;
}
}
}
子适配器示例:
public class Meeting1Adapter: MvxRecyclerAdapter
{
private Activity activity;
public MeetingsMain meetingMain { get; set; }
public Meeting1Adapter(IMvxAndroidBindingContext bindingContext, Activity activity, MeetingsMain meetingsMain)
: base(bindingContext)
{
this.activity = activity;
this.meetingMain = meetingsMain;
}
public void RefreshItems(Object response)
{
if (response is List<ContactDetails>)
meetingMain.Meetings = (List<MeetingDetails>)response;
NotifyDataSetChanged();
}
public override int ItemCount
{
get { return meetingMain.Meetings.Count; }
}
public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
var meeting = holder as MeetingsAdapterValueTypeViewHolder;
meeting.txtMeg.Text = meetingMain.Meetings[position].MeetingTitle;
}
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
try
{
var itemBindingContext = new MvxAndroidBindingContext(parent.Context, this.BindingContext.LayoutInflaterHolder);
var id = Resource.Layout.item_layout;
View itemView = LayoutInflater.From(parent.Context).Inflate(id, parent, false);
return new MeetingsAdapterValueTypeViewHolder(itemView, itemBindingContext);
}catch(Exception e)
{
Console.WriteLine("Exception in response ## " + e.Message);
}
return null;
}
public class MeetingsAdapterValueTypeViewHolder : MvxRecyclerViewHolder
{
// public ImageView Image { get; private set; }
public TextView txtMeg { get; private set; }
public MeetingsAdapterValueTypeViewHolder(View itemView, IMvxAndroidBindingContext ctxt) : base(itemView,ctxt)
{
txtMeg = itemView.FindViewById<TextView>(Resource.Id.textView);
}
}
}
}
问题:在子适配器中,OnCreateViewHolder()方法没有调用。
提前致谢。
答案 0 :(得分:0)
在搜索解决方案2天后,我终于得到了结果。
加载MvxRecyclerview时我们应该检查的主要内容是
1: MvxRecyclerView.LayoutManager.
2:In Child Adapter,Since we are using MvxRecyclerView,we should use IMvxAndroidBindingContext
and the associated adapter changes like
a:Adapter should extend MvxRecyclerAdapter
b: use MvxRecyclerViewHolder
------------------------------------------编辑你的适配器采用以下方式----
MyRecyler1Adapter中的代码更改:
MvxRecyclerView.LayoutManager layoutManager;//Dont use RecyclerView.LayoutManager
在OnBindViewHolder()
中 layoutManager = new LinearLayoutManager(_activity, LinearLayoutManager.Vertical, false);
meeting.recycle.SetLayoutManager(layoutManager);
Inside Child Adapter:
public class Meeting1Adapter: MvxRecyclerAdapter
{
private Activity activity;
public IMvxAndroidBindingContext bindingCtxt;
protected IMvxAndroidBindingContext BindingContext => bindingCtxt;
public MeetingsMain meetingMain { get; set; }
public Meeting1Adapter(IMvxAndroidBindingContext bindingContext, Activity activity, MeetingsMain meetingsMain)
{
this.bindingCtxt = bindingContext;
this.activity = activity;
this.meetingMain = meetingsMain;
}
----
public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
try
{
var itemBindingContext = new MvxAndroidBindingContext(parent.Context, this.BindingContext.LayoutInflaterHolder);
var id = Resource.Layout.item_layout;
View itemView = LayoutInflater.From(parent.Context).Inflate(id, parent, false);
return new MeetingsAdapterValueTypeViewHolder(itemView, itemBindingContext);
// return new MeetingsAdapterValueTypeViewHolder(itemView);
}
catch (Exception e)
{
Console.WriteLine("Exception in response ## " + e.Message);
}
return null;
}
----
}