gpt4 book ai didi

c# - 仅显示列表中的一个项目

转载 作者:行者123 更新时间:2023-12-03 10:53:21 25 4
gpt4 key购买 nike

我有以下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;
});
}
}

示例ViewPagerFragment.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;
}
}
}

这是当前输出。

enter image description here

更新:

这是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个ViewModel?

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

整数将是数组的索引。

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]

关于c# - 仅显示列表中的一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35926301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com