仅显示列表中的单个项目
Showing only a single item from the List
我有以下模型 class,列表中有 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;
});
}
}
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;
}
}
}
这是当前输出。
更新:
这是 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>
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>
我认为您最安全的选择可能是拥有 5 个视图模型?
如果你不想要那个..你可以创建一个静态 class 来保存一个整数和一个数组
整数将作为数组的索引。
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++;
你还是得测试一下。我猜比赛调节可能是个问题.. :)
编辑:
为了避免越界,你可以使用
Title = StaticClass.a[StaticClass.index < StaticClass.a.Length ? StaticClass.Index : StaticClass.a.Length -1]
您或许可以创建自定义绑定以将参数传递给 ItemsSource。
这是绑定到我的视图模型中的 属性 的自定义绑定(摘要)示例:
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 将是一个 MvxRecyclerView,您可以在其中设置 ItemsSource。
我有以下模型 class,列表中有 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;
});
}
}
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;
}
}
}
这是当前输出。
更新:
这是 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>
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>
我认为您最安全的选择可能是拥有 5 个视图模型?
如果你不想要那个..你可以创建一个静态 class 来保存一个整数和一个数组
整数将作为数组的索引。
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++;
你还是得测试一下。我猜比赛调节可能是个问题.. :)
编辑:
为了避免越界,你可以使用
Title = StaticClass.a[StaticClass.index < StaticClass.a.Length ? StaticClass.Index : StaticClass.a.Length -1]
您或许可以创建自定义绑定以将参数传递给 ItemsSource。
这是绑定到我的视图模型中的 属性 的自定义绑定(摘要)示例:
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 将是一个 MvxRecyclerView,您可以在其中设置 ItemsSource。