我在viewmodel中有五个对象如下,我想拥有相同数量的片段。目前,我已经硬编码了五个碎片。
是否有办法从viewmodel获取对象的数量并将其传递给视图以使其模块化而不是硬编码。我正在使用mvvm模式。
ViewModel类
NAMES VALUE1 VALUE2 VALUE3
john 1 2 3
bro 4 5 6
guy 7 8 9
查看课程
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" }
};
}
答案 0 :(得分:2)
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);
和
'$stateChangeSuccess'
就像我之前所说的,种族调节可能会导致问题,但要测试一下:)
答案 1 :(得分: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);
}
}
}
构建界面的viewmodel:
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;
}