MvvmCross:基于片段标签视图中的多态列表 [英] MvvmCross: Polymorphic list within fragment based tab view

查看:150
本文介绍了MvvmCross:基于片段标签视图中的多态列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个使用基于片段TabView的一个GUI。在code是基于MvvmCross教程中发现的FragmentSample。在TabView的内翼片的数量取决于数据从XML文件中读取与每个标签使用相同的片段。到目前为止,一切都workes确定。当我试图多态列表添加到碎片的问题开始。用于创建polymorhpic列表中的code是基本相同(从MvvmCross再次教程)收集样本中发现的。

I am trying to create a GUI which uses fragment based TabView. The code is based on the FragmentSample found in MvvmCross tutorials. The number of tabs within the TabView depends on data read from xml file and every tab uses the same fragment. So far everything workes OK. The problems started when I tried to add polymorphic list to the fragment. The code used to create polymorhpic list is basically the same as found in Collection sample(again from MvvmCross tutorials).

首先,我试图创建列表适配器,并将其与TabView的(类扩展MvxTabsFragmentActivity)相关的活动中分配给MvxListView。在I类覆盖OnViewModelSet()方法。在覆盖创建延伸MvxAdapter并将其分配给列表中的ListAdapter。问题是,GetBindableView(查看convertedView,对象的datacontext,INT templateId)在适配器不会被调用所以列表中的所有项目都显示方法通过调用绑定到列表项的ToString()方法。也许这是值得一提的是,如果我.axml文件中定义的DataTemplate列表中工作正常 - 但不是多态。

First i tried to create the list adapter and assign it to the MvxListView within Activity associated with the TabView(class extending MvxTabsFragmentActivity). In that class I override the OnViewModelSet() method. Within the override I create an ListAdapter which extends MvxAdapter and assign it to the list. The problem is that the GetBindableView(View convertedView, object dataContext, int templateId) method in the adapter never gets called so all items in the list are shown by calling the ToString() method on items bound to the list. Maybe it is worth mentioning that if I define the DataTemplate within the .axml file the list works ok - but is not polymorphic.

然后我试图移动code用于创建ListAdapter与片段相关联的类 - 从MvxFragment来源之一 - 由于该表位于片段里,这似乎更好。因为我不知道重写哪种方法我第一次尝试了OnAttach方法,取得了一个异常说的BindingContext不能在这个时候可以使用。当我把code中的片段东西resume()方法终于开始工作。为了创建我只是用适配器时所需的上下文实例base.Activity。

Then I tried to move the code used to create the ListAdapter to the class associated with the fragment - the one derived from MvxFragment - which seems better since the list is located inside the fragment. Since I wasn't sure which method to override I first tried the OnAttach method which yielded in an exception saying that the BindingContext cannot be used at this time. When I put the code in the Resume() method of the fragment things finally started working. For the Context instance needed when creating the adapter I just used the base.Activity.

所以最后我的问题。以上OK描述的解决方案 - 是OnResume()方法来创建列表适配器或我应该使用一些不同的方法。正如我所说的 - 一切似乎都工作正常,我只是不知道这是否是最好的(好)解决方案

So finally to my question. Is the solution described above OK - is the OnResume() method the one to create the list adapter or should I use some different approach. As I said - everything seem to be working OK I am just not sure if this is the best(good) solution.

我希望帖子不是长 - 我试图尽可能简短,而不忽略的重要细节。任何意见将是AP preciated。

I hope the post is not to long - I tried to be as brief as possible without omitting important details. Any advice will be appreciated.

乌罗什

推荐答案

是的,正如斯图尔特所说,这是更好地做到这一点在OnCreateView()方法。

Yes, just as Stuart mentioned, it is better to do it in the OnCreateView() method.

我设法做这样的:

public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    base.OnCreateView (inflater, container, savedInstanceState);
    View fragmentView = this.BindingInflate(Resource.Layout.View_Applications, null);
    var list = fragmentView.FindViewById<MvxListView>(Resource.Id.TheListView);
    list.Adapter = new HeaderAdapter(Activity, (IMvxAndroidBindingContext) BindingContext);
    return fragmentView;
}

看一看斯图尔特的例子有收藏了解更多详情。

这篇关于MvvmCross:基于片段标签视图中的多态列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆