Create a number of fragments to match number of ob

2019-09-16 19:13发布

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);
 }

标签: c# mvvm xamarin
2条回答
Melony?
2楼-- · 2019-09-16 19:52
public static class StaticClass
{
    public static String[] a= {"A","B","C","D","E"};
    public static int index = 0;
}

so in your constructor:

Items = new ObservableCollection<ListItem> {
                new ListItem { StaticClass.a[StaticClass.index < StaticClass.a.Length ? StaticClass.Index : StaticClass.a.Length -1] }
            };
StaticClass.Index++;

and:

var fragments = new List<MvxFragmentPagerAdapter.FragmentInfo>();

    for(int i = 0; i < StaticClass.a.Length; i++
         {
          fragments.add(
          new MvxFragmentPagerAdapter.FragmentInfo("RecyclerView " + (i+1).ToString(), typeof (RecyclerViewFragment),typeof (RecyclerViewModel));
         }
    viewPager.Adapter = new MvxFragmentPagerAdapter(Activity, ChildFragmentManager, fragments);

Like i said before race conditioning could cause a problem but test it out :)

查看更多
可以哭但决不认输i
3楼-- · 2019-09-16 20:02

I've done something similar with tabs, with a dynamic amount of tabs, here is the code dump:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <include
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        layout="@layout/merge_header" />    
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

View:

[Activity(ParentActivity = typeof(HomeView), Theme = "@style/Theme.App", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
public class PlanningView : BaseTabActivity<PlanningViewModel>
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.page_planning);

        ViewModel.BuildInterface();

        CreateTabs();
    }

    void CreateTabs()
    {
        TabHost.TabSpec spec;

        spec = TabHost.NewTabSpec(ViewModel.TextSource.GetText(LocalizationConstants.Planning_Stats));

        spec.SetIndicator(ViewModel.TextSource.GetText(LocalizationConstants.Planning_Stats));

        var intent = new Intent(this.CreateIntentFor(ViewModel.PlanningStats));

        spec.SetContent(intent.AddFlags(ActivityFlags.ClearTop));

        TabHost.AddTab(spec);

        var currentGameweek = ViewModel.CurrentGW;

        foreach (var planningGW in ViewModel.PlanningGW)
        {
            spec = TabHost.NewTabSpec(string.Format("{0}{1}", ViewModel.SharedTextSource.GetText(LocalizationConstants.Shared_GW), planningGW.Gameweek));
            spec.SetIndicator(string.Format("{0}{1}", ViewModel.SharedTextSource.GetText(LocalizationConstants.Shared_GW), planningGW.Gameweek));

            intent = new Intent(this.CreateIntentFor(planningGW));

            spec.SetContent(intent.AddFlags(ActivityFlags.ClearTop));

            TabHost.AddTab(spec);
        }
    }
}

viewmodel to build interface:

PlanningStatsViewModel _planningStats;
    public PlanningStatsViewModel PlanningStats
    {
        get { return _planningStats; }
        set { _planningStats = value; RaisePropertyChanged(() => PlanningStats); }
    }

    ObservableCollection<PlanningGWViewModel> _planningGW;
    public ObservableCollection<PlanningGWViewModel> PlanningGW
    {
        get { return _planningGW; }
        set { _planningGW = value; RaisePropertyChanged(() => PlanningGW); }
    }

public void BuildInterface()
    {
        PlanningStats = new PlanningStatsViewModel(_dataService, _loadingService, _messenger, _messageService);
        PlanningGW = new ObservableCollection<PlanningGWViewModel>();

        var nonPlayedGameweeks = Service.GetCacheNonPlayedGameweeksAsync().Result;
        var query = nonPlayedGameweeks.ToList();

        if (query.Any())
        {
            CurrentGW = query.Min(f => f.GameWeek);

            var grouped = query.ToList().GroupBy(f => f.GameWeek, f => f, (key, g) => new { Gameweek = key, Fixtures = g.ToList() });

            foreach (var grp in grouped.Take(SettingsPreferences.SelectedPlanUpcoming))
                if (grp.Gameweek <= BusinessConstants.NumGamesSeason)
                    PlanningGW.Add(new PlanningGWViewModel(grp.Gameweek, _dataService, _loadingService, _messenger, _messageService));
        }
    }

each tabs viewmodel:

ObservableCollection<PlanningGWItemsViewModelWrapper> _planningGWItems;
    public ObservableCollection<PlanningGWItemsViewModelWrapper> PlanningGWItems
    {
        get { return _planningGWItems; }
        set { _planningGWItems = value; RaisePropertyChanged(() => PlanningGWItems); }
    }

public async Task RebuildLists(bool displayLoading)
    {
        IsLoading = displayLoading;

        Standings = await Service.GetCacheStandingsAsync().ConfigureAwait(false);

        var nonPlayedGameweeks = await Service.GetCacheNonPlayedGameweeksAsync().ConfigureAwait(false);

        PlanningGWItems = await BuildList(Standings, nonPlayedGameweeks.Where(f => f.GameWeek == _gameweek).OrderBy(f => f.Date).ToList()).ConfigureAwait(false);

        IsLoading = false;
    }
查看更多
登录 后发表回答