仅显示列表中的一个项目

cas*_*las 0 c# mvvm mvvmcross xamarin

我有以下Model类,它在列表中有5个对象。我不想在一个页面上显示所有这些内容,而是想在第一个片段中显示第一项,在第二个片段中显示第二项,依此类推。

我正在使用mvvm模式。

RecyclerViewModel.cs

namespace Example.Core.ViewModels
{
    public class RecyclerViewModel
        : MvxViewModel
    {
        private ListItem _selectedItem;

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

        private ObservableCollection<ListItem> _items;

        public ObservableCollection<ListItem> Items
        {
            get { return _items; }
            set
            {
                _items = value;
                RaisePropertyChanged(() => Items);
            }
        }

        public ListItem SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                _selectedItem = value;
                RaisePropertyChanged(() => SelectedItem);
            }
        }

        public virtual ICommand ItemSelected
        {
            get
            {
                return new MvxCommand<ListItem>(item =>
                {
                    SelectedItem = item;
                });
            }
        }

        private bool _isRefreshing;

        public virtual bool IsRefreshing
        {
            get { return _isRefreshing; }
            set
            {
                _isRefreshing = value;
                RaisePropertyChanged(() => IsRefreshing);
            }
        }

        public ICommand ReloadCommand
        {
            get
            {
                return new MvxCommand(async () =>
                {
                    IsRefreshing = true;

                    await ReloadData();

                    IsRefreshing = false;
                });
            }
        }
Run Code Online (Sandbox Code Playgroud)

ExampleViewPagerFragment.cs

namespace Example.Droid.Fragments
{
    [MvxFragment(typeof (MainViewModel), Resource.Id.content_frame)]
    [Register("example.droid.fragments.ExampleViewPagerFragment")]
    public class ExampleViewPagerFragment : BaseFragment<ExampleViewPagerViewModel>
    {
        protected override int FragmentId => Resource.Layout.fragment_example_viewpager;

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = base.OnCreateView(inflater, container, savedInstanceState);

            var viewPager = view.FindViewById<ViewPager>(Resource.Id.viewpager);
            if (viewPager != null)
            {
                var fragments = new List<MvxFragmentPagerAdapter.FragmentInfo>
                {
                    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);
            }

            var tabLayout = view.FindViewById<TabLayout>(Resource.Id.tabs);
            tabLayout.SetupWithViewPager(viewPager);

            return view;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是当前输出。

在此处输入图片说明


更新:

这是XML文件

fragmentviewpager.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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Jes*_*sen 5

我认为您最安全的选择可能是拥有5个ViewModel?

如果您不想要..您可以创建一个静态类来保存一个整数和一个数组

整数将是数组的索引。

public static class StaticClass
{
    public static String[] a= {"A","B","C","D","E"};
    public static int index = 0;
}
Run Code Online (Sandbox Code Playgroud)

因此在您的构造函数中:

Items = new ObservableCollection<ListItem> {
                new ListItem { Title = StaticClass.a[StaticClass.index] }
            };
StaticClass.Index++;
Run Code Online (Sandbox Code Playgroud)

您必须测试它。我猜想竞赛条件可能是个问题.. :)

编辑:

为了避免超出范围,您可以使用

Title = StaticClass.a[StaticClass.index < StaticClass.a.Length ? StaticClass.Index : StaticClass.a.Length -1]
Run Code Online (Sandbox Code Playgroud)