Search code examples
androidxamarinmvvmcross

MvvmCross ViewPager not show in Activity


I want to show a few tabs sharing the same viewmodel with viewpager in an activity. I am using MvvmCross 5.6.2.

When I do that in a fragment, the viewpager is shown properly. Using the same method, I add tab fragments to the viewpager in an activity with code like this:

        var viewPagerFragmentList = new List<MvxViewPagerFragmentInfo>
                {
                    new MvxViewPagerFragmentInfo("Personal", typeof (SettingPersonalFragment), ViewModel),
                    new MvxViewPagerFragmentInfo("Preference", typeof (SettingPreferenceFragment), ViewModel)
                };
        viewPager.Adapter = new MvxCachingFragmentStatePagerAdapter(this, SupportFragmentManager, viewPagerFragmentList);

        var tabLayout = FindViewById<TabLayout>(Resource.Id.tabs_fragment_setting_viewpager);
        tabLayout.SetupWithViewPager(viewPager);

The tab fragments are not shown on screen, though the tab fragment's OnCreate and OnCreateView are executed.

I found the latest sample Playground is showing tabs in activity too. Following the sample, I register the fragment:

    [MvxTabLayoutPresentation(TabLayoutResourceId = Resource.Id.tabs_fragment_setting_viewpager, ViewPagerResourceId = Resource.Id.viewpager_fragment_setting_viewpager, Title = "Personal", ActivityHostViewModelType = typeof(SettingViewModel))]

No luck with that. The last thing I found in Playground to show a tab is to navigate to the fragment viewmodel:

    _navigationService.Navigate<Tab2ViewModel>();
    _navigationService.Navigate<Tab3ViewModel>();

However, in my case, I do not have a viewmodel for each fragment. The workaround on my hand is to move the viewpager to a fragment in the activity.

How could I show the viewpager fragments without their own viewmodels in an activity?

Thanks,

Nick


Solution

  • The above code that works in fragment also work in activity. There is an issue in the xml causing the viewpager not showing. Sorry.

    Thanks everyone who helped.