Iam目前正在尝试在我的应用中为平板电脑布局实现复合视图,所以我不需要在手机布局中使用的Viewpager和TabLayout。
但显然它并不像我想象的那么容易,我找到的示例/教程是针对旧版MVVMCross而制作的。
我的想法只是搜索viewpager,如果在当前布局中找不到,则检查framelayouts并显示我的两个不同的视图模型,就像我使用Viewpager / Tablayout的MvxFragmentPagerAdapter一样。
有没有"清洁"解决方案?
谢谢你的时间!
答案 0 :(得分:0)
我使用FragmentTransactions找到了可接受的答案。这是简化的代码。虽然iam有点滥用MvxFragmentPagerAdapter.GetItem(index)来处理Fragment和ViewModel Initialization(https://github.com/MvvmCross/MvvmCross-AndroidSupport/blob/master/MvvmCross.Droid.Support.V4/MvxFragmentPagerAdapter.cs):
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = base.OnCreateView(inflater, container, savedInstanceState);
var viewPager = view.FindViewById<ViewPager>(Resource.Id.viewpager);
var fragments = new List<MvxFragmentPagerAdapter.FragmentInfo> {
new MvxFragmentPagerAdapter.FragmentInfo("Fragment1", typeof(FragmentOne),
typeof(FragmentViewModelOne)),
new MvxFragmentPagerAdapter.FragmentInfo("Fragment2", typeof(FragmentTwo),
typeof(FragmentViewModelTwo))
};
var adapter = new MvxFragmentPagerAdapter(Activity, ChildFragmentManager, fragments);
if (viewPager != null)
{
viewPager.Adapter = adapter;
var tabLayout = view.FindViewById<TabLayout>(Resource.Id.tabs);
if (tabLayout != null)
{
tabLayout.SetupWithViewPager(viewPager);
}
}
else
{
FragmentTransaction transaction = FragmentManager.BeginTransaction();
transaction.Replace(Resource.Id.frameone, adapter.GetItem(0), "Fragment1");
transaction.Replace(Resource.Id.frametwo, adapter.GetItem(1), "Fragment2");
transaction.Commit();
}
return view;
}
如果有更好的解决方案,请不要害羞! =)
Greetings Cyriac