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).
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.
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.
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.
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.
Uros
I don't think I've fully understood what you were trying to initialise where, nor which viewmodels you were assigning to which activity, fragment and list.
However, as a basic answer I'd typically expect you to create your listview and it's adapter within the
OnCreateView
creation of the Fragment - this occurs after onAttach.Within the Mvx source, this is where we try to create the Fragment's BindingContext - see https://github.com/slodge/MvvmCross/blob/v3/Cirrious/Cirrious.MvvmCross.Droid.Fragging/Fragments/MvxBindingFragmentAdapter.cs#L32
I think you might be OK using
OnResume
- although you might discover this can get called more than once for a given created fragment view - e.g. you will get called if the currently owning Activity is paused and then resumed. Because of this I think you will probably be more successful if you choose to overrideOnCreateView
There's quite a lot to grasp with Fragment lifecycle - especially as it is different to Activity lifecycle (Fragments can and do live longer than the Activities they live in)
The documentation on http://developer.android.com/guide/components/fragments.html is a good start - including this picture:
Yes, just as Stuart mentioned, it is better to do it in the OnCreateView() method.
I managed doing it like this:
Have a look at Stuart's example with collections for more details.