仅显示列表中的单个项目

时间:2016-03-10 20:12:44

标签: c# mvvm xamarin mvvmcross

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

我正在使用mvvm模式。

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

ExampleViewPagerFragment.cs

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

这是当前的输出。

enter image description here

更新

这是xmls

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

recyclerview.xml

$ var="COLUMN_NAME||','||"
$ echo "${var%???????}"
COLUMN_NAME

2 个答案:

答案 0 :(得分:4)

我认为你最安全的赌注可能是拥有5个视图模型?

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

整数将是您的数组的索引。

import random

list = [1, 2, 3, 4]

random_elements = [random.choice(list) for n in range(5)]

所以在你的构造函数中:

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

你必须测试它。比赛调节可能是一个问题,我猜..:)

编辑:

为了避免越界,你可以使用

Items = new ObservableCollection<ListItem> {
                new ListItem { Title = StaticClass.a[StaticClass.index] }
            };
StaticClass.Index++;

答案 1 :(得分:0)

您可以创建自定义绑定以将参数传递给ItemsSource。

以下是绑定到viewmodel中属性的自定义绑定(摘要)的示例:

local:MvxBind="Text SummaryEnum3Int; Summary SummaryEnum3;" />

在设置中:

protected override void FillTargetFactories(IMvxTargetBindingFactoryRegistry registry)
{
    base.FillTargetFactories(registry);

    registry.RegisterFactory(new MvxCustomBindingFactory<TextView>("Summary", textView => new SummaryTextViewBinding(textView)));
}

和自定义绑定:

public class SummaryTextViewBinding : MvxAndroidTargetBinding
{
    readonly TextView _textView;
    SummaryEnumeration _currentValue;

    public SummaryTextViewBinding(TextView textView) : base(textView)
    {
        _textView = textView;
    }

    #region implemented abstract members of MvxConvertingTargetBinding
    protected override void SetValueImpl(object target, object value)
    {            
        if (!string.IsNullOrEmpty(_textView.Text))
        {
            _currentValue = (SummaryEnumeration)Convert.ToInt32(_textView.Text);

            SetTextViewBackground();
        }
    }
    #endregion

    void SetTextViewBackground()
    {
        switch (_currentValue)
        {
            case SummaryEnumeration.Easy:
                _textView.SetBackgroundResource(Resource.Drawable.background_circle_green);
                _textView.Text = string.Empty;

                break;
            case SummaryEnumeration.Medium:
                _textView.SetBackgroundResource(Resource.Drawable.background_circle_yellow);
                _textView.Text = string.Empty;

                break;
            case SummaryEnumeration.Difficult:
                _textView.SetBackgroundResource(Resource.Drawable.background_circle_red);
                _textView.Text = string.Empty;

                break;
            case SummaryEnumeration.None:
                _textView.SetBackgroundResource(Resource.Drawable.background_circle_none);
                _textView.Text = LocalizationConstants.Nothing;

                break;
        }
    }

    public override Type TargetType
    {
        get { return typeof(bool); }
    }

    public override MvxBindingMode DefaultMode
    {
        get { return MvxBindingMode.OneWay; }
    }
}

所以你可能会在每个片段(括号中的片段编号)中得到类似的东西:

local:MvxBind="ItemsSource FilteredItems; CurrentFragment(1); ItemClick ItemSelected"

FilteredItems将根据您所在的片段进行过滤,在自定义绑定中进行过滤。在您的情况下,我的textview将是您设置ItemsSource的MvxRecyclerView。