2016-03-14 67 views
0

我的Viewmodel中有五個對象,如下所示。我想讓這個對象在ViewPager的每個視圖中顯示出來。因此,我用五個片段硬編碼。MVVM模式下的Viewpager

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

這裏是我的項目的github:https://github.com/texas16/ViewPagerMVVM

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

ViewPager.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      local:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
      local:layout_scrollFlags="scroll|enterAlways" /> 
     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingLeft="16dp" 
      local:tabGravity="center" 
      local:tabMode="scrollable" /> 
    </android.support.design.widget.AppBarLayout> 
    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     local:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
</android.support.design.widget.CoordinatorLayout> 

fragment_recyclerview.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout 
     android:id="@+id/refresher" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     local:layout_behavior="@string/appbar_scrolling_view_behavior" 
     local:MvxBind="Refreshing IsRefreshing; RefreshCommand ReloadCommand"> 
     <MvxRecyclerView 
      android:id="@+id/my_recycler_view" 
      android:scrollbars="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      local:MvxItemTemplate="@layout/listitem_recyclerviewexample" 
      local:MvxBind="ItemsSource Items; ItemClick ItemSelected" /> 
    </MvvmCross.Droid.Support.V4.MvxSwipeRefreshLayout> 
</android.support.design.widget.CoordinatorLayout> 

listitem_recyclerviewexample.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
     android:id="@+id/innerText" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_gravity="center" 
     android:layout_marginRight="5dp" 
     android:padding="10dp" 
     android:textSize="60sp" 
     local:MvxBind="Text Title" /> 
</LinearLayout> 

回答

1

我看到可能與它的結構是在此之前作出新的視圖模型的方式的唯一途徑部分:

var fragments = new List<MvxFragmentPagerAdapter.FragmentInfo> 
    { 
     // hard coded 
     ... 
     ... 
     ... 
    }; 

這樣:

var vm = new RecyclerViewModel(); 
    var fragments = new List<MvxFragmentPagerAdapter.FragmentInfo>(); 
    for(int i = 0; i < vm.Items.Count; i++) 
    { 
     fragments.Add(new MvxFragmentPagerAdapter.FragmentInfo("RecyclerView " + (i+1).ToString(), typeof (RecyclerViewFragment),typeof (RecyclerViewModel))); 
    } 

    viewPager.Adapter = new MvxFragmentPagerAdapter(Activity, ChildFragmentManager, fragments); 
+0

非常感謝加斯帕這裏是我的項目github上:https://github.com/texas16/ViewPagerMVVM,我會很高興,如果你能引導我從這個小項目。當您運行項目時,您可能需要點擊導航抽屜並點擊ExampleViewPager查看我的問題。我已經提高了你最初的努力。 – hotspring