我正在编写带有标签的Android应用。我正在按照样本中的旧方法" FragmentSample"。它工作正常,但我正在评估切换到viewpager。
在FragmentSample中:
这是完美的,因为我可以对Vm1进行一些导航初始化, TabViewModel中的Vm2。
public class TabViewModel : BaseViewModel
{
public TabViewModel()
{
Vm1 = Mvx.IocConstruct<FirstTabViewModel>();
Vm2 = Mvx.IocConstruct<SecondTabViewModel>();
Vm3 = Mvx.IocConstruct<ThirdTabViewModel>();
}
public BaseViewModel Vm1 { get; set; }
public BaseViewModel Vm2 { get; set; }
public BaseViewModel Vm3 { get; set; }
}
public class TabView : MvxTabsFragmentActivity
{
public TabViewModel TabViewModel
{
get { return (TabViewModel)base.ViewModel; }
}
public TabView()
: base(Resource.Layout.Page_TabView, Resource.Id.actualtabcontent)
{
}
protected override void AddTabs(Bundle args)
{
AddTab<Tab1Fragment>("Tab1", "Tab 1", args, TabViewModel.Vm1);
AddTab<Tab2Fragment>("Tab2", "Tab 2", args, TabViewModel.Vm2);
// note that
AddTab<Tab3Fragment>("Tab3.1", "Tab 3.1", args, TabViewModel.Vm3);
AddTab<Tab3Fragment>("Tab3.2", "Tab 3.2", args, TabViewModel.Vm3);
AddTab<Tab3BigFragment>("Tab3.3", "Tab 3.3", args, TabViewModel.Vm3);
}
}
在最新的示例项目&#34;示例&#34;在MvvmCross-All:
RecyclerViewModel创建了两次。它是一样的 ExampleViewPagerFragment在同一个项目中,在旧版本中 这个样本XPlatformMenus。
public class ExampleViewPagerStateViewModel
: MvxViewModel
{
public RecyclerViewModel Recycler { get; private set; }
public ExampleViewPagerStateViewModel()
{
Recycler = new RecyclerViewModel();
}
}
public class ExampleViewPagerStateFragment : BaseStateFragment<ExampleViewPagerStateViewModel>
{
protected override int FragmentId => Resource.Layout.fragment_example_viewpager_state;
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);
if (viewPager != null)
{
var fragments = new List<MvxCachingFragmentStatePagerAdapter.FragmentInfo>
{
//new MvxCachingFragmentStatePagerAdapter.FragmentInfo("RecyclerView 1", typeof (RecyclerViewFragment),
// typeof (RecyclerViewModel)),
//new MvxCachingFragmentStatePagerAdapter.FragmentInfo("RecyclerView 2", typeof (RecyclerViewFragment),
// typeof (RecyclerViewModel)),
//new MvxCachingFragmentStatePagerAdapter.FragmentInfo("RecyclerView 3", typeof (RecyclerViewFragment),
// typeof (RecyclerViewModel)),
//new MvxCachingFragmentStatePagerAdapter.FragmentInfo("RecyclerView 4", typeof (RecyclerViewFragment),
// typeof (RecyclerViewModel)),
new MvxCachingFragmentStatePagerAdapter.FragmentInfo("RecyclerView 5", typeof (RecyclerViewFragment),
typeof (RecyclerViewModel))
};
viewPager.Adapter = new MvxCachingFragmentStatePagerAdapter(Activity, ChildFragmentManager, fragments);
}
var tabLayout = view.FindViewById<TabLayout>(Resource.Id.tabs);
tabLayout.SetupWithViewPager(viewPager);
return view;
}
}
我的问题是:
感谢。
答案 0 :(得分:0)
刚刚发现可以通过将FragmentInfo的第三个参数更改为RecyclerViewModel中创建的Recycler来完成。样本应该做出改变。