【问题标题】:Viewpager in MVVM patternMVVM 模式中的 Viewpager
【发布时间】:2016-06-29 22:20:42
【问题描述】:

我的 Viewmodel 中的 List 中有五个对象,如下所示。我希望每个对象都显示在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" }
    };
 }

查看类

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>

【问题讨论】:

    标签: c# android mvvm xamarin


    【解决方案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);
    

    【讨论】:

    • 非常感谢 Jesper 这是我的项目 github:github.com/texas16/ViewPagerMVVM,如果你能指导我完成这个小项目,我会很高兴。运行项目时,您可能需要单击导航抽屉并单击 ExampleViewPager 以查看我的问题。我赞成你最初的努力。
    猜你喜欢
    • 2018-06-30
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多