I have five objects as follows in the viewmodel, and I want to have the same number of fragments. At the moment, I have hardcoded with five fragments.
Is there a way to get the number of objects from the viewmodel and pass it to the view to make it modular rather than hard coded. I am using mvvm pattern.
ViewModel class
public RecyclerViewModel()
{
Items = new ObservableCollection<ListItem> {
new ListItem { Title = "A" },
new ListItem { Title = "B" },
new ListItem { Title = "C" },
new ListItem { Title = "D" },
new ListItem { Title = "E" }
};
}
View class
var viewPager = view.FindViewById<ViewPager>(Resource.Id.viewpager);
if (viewPager != null)
{
var fragments = new List<MvxFragmentPagerAdapter.FragmentInfo>
{
// hard coded
new MvxFragmentPagerAdapter.FragmentInfo("RecyclerView 1", typeof (RecyclerViewFragment),typeof (RecyclerViewModel)),
new MvxFragmentPagerAdapter.FragmentInfo("RecyclerView 2", typeof (RecyclerViewFragment),typeof (RecyclerViewModel)),
new MvxFragmentPagerAdapter.FragmentInfo("RecyclerView 3", typeof (RecyclerViewFragment),typeof (RecyclerViewModel)),
new MvxFragmentPagerAdapter.FragmentInfo("RecyclerView 4", typeof (RecyclerViewFragment),typeof (RecyclerViewModel)),
new MvxFragmentPagerAdapter.FragmentInfo("RecyclerView 5", typeof (RecyclerViewFragment), typeof (RecyclerViewModel))
};
viewPager.Adapter = new MvxFragmentPagerAdapter(Activity, ChildFragmentManager, fragments);
}
so in your constructor:
and:
Like i said before race conditioning could cause a problem but test it out :)
I've done something similar with tabs, with a dynamic amount of tabs, here is the code dump:
View:
viewmodel to build interface:
each tabs viewmodel: