2016-03-10 16 views
0

我在視圖模型中有如下五個對象,並且我希望片段數量相同。目前,我已經用五段硬編碼。創建若干片段以匹配ViewModel中對象的數量

有沒有辦法從視圖模型中獲取對象的數量並將其傳遞給視圖以使其模塊化而不是硬編碼。我正在使用mvvm模式。

ViewModel類

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類

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

回答

2
public static class StaticClass 
{ 
    public static String[] a= {"A","B","C","D","E"}; 
    public static int index = 0; 
} 

所以在構造函數:

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

和:

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

就像我說的比賽前調理可能會導致一個問題,但測試出來:)

+0

嗨加斯帕,它的工作原理,非常感謝您的快速答覆。我只是想知道是否有一種處理方式而不創建靜態類? – hotspring

0

我已經做了標籤類似的東西,使用標籤的動態量,這裏是代碼轉儲:

<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> 

查看:

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

視圖模型建立接口:

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

每個標籤視圖模型:

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