gpt4 book ai didi

c# - Xamarin 中 MVVMCross 模式中的 RecyclerView 复选框

转载 作者:搜寻专家 更新时间:2023-11-01 07:49:47 24 4
gpt4 key购买 nike

我有以下 .axmls,它基本上是一个包含 TextView 和复选框的 recyclerview。我无法弄清楚如何与复选框交互以删除 mvvmcross 设计中的选定项目。

fragment_example_recyclerview.axml

<?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">
<include
layout="@layout/toolbar_actionbar" />
<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" />
</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="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<CheckBox android:id="@+id/checkbox_meat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/innerText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:padding="10dp"
local:MvxBind="Text Title" />

ExampleViewPagerViewModel.cs

using MvvmCross.Core.ViewModels;

namespace Example.Core.ViewModels
{
public class ExampleViewPagerViewModel
: MvxViewModel
{
public RecyclerViewModel Recycler { get; private set; }

public ExampleViewPagerViewModel()
{
Recycler = new RecyclerViewModel();
}
}
}

RecyclerViewModel.cs

using MvvmCross.Core.ViewModels;
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Input;

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

ExampleViewPagerFragment.cs

using System.Collections.Generic;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V4.View;
using Android.Views;
using Example.Core.ViewModels;
using MvvmCross.Droid.Support.V4;
using MvvmCross.Droid.Support.V7.Fragging.Attributes;

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

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

return view;
}
}
}

RecyclerViewFragment.cs

using System;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
using MvvmCross.Platform.WeakSubscription;
using MvvmCross.Binding.Droid.BindingContext;
using MvvmCross.Droid.Support.V7.Fragging.Fragments;
using MvvmCross.Droid.Support.V7.RecyclerView;
using MvvmCross.Droid.Support.V4;
using Example.Core.ViewModels;
using MvvmCross.Droid.Support.V7.Fragging.Attributes;

namespace Example.Droid.Fragments
{
[MvxFragment(typeof(MainViewModel), Resource.Id.content_frame)]
[Register("example.droid.fragments.RecyclerViewFragment")]
public class RecyclerViewFragment : MvxFragment<RecyclerViewModel>
{
private IDisposable _itemSelectedToken;

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

var view = this.BindingInflate(Resource.Layout.fragment_recyclerview, null);

var recyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.my_recycler_view);
if (recyclerView != null)
{
recyclerView.HasFixedSize = true;
var layoutManager = new LinearLayoutManager(Activity);
recyclerView.SetLayoutManager(layoutManager);
}

_itemSelectedToken = ViewModel.WeakSubscribe(() => ViewModel.SelectedItem,
(sender, args) => {
if (ViewModel.SelectedItem != null)
Toast.MakeText(Activity,
$"Selected: {ViewModel.SelectedItem.Title}",
ToastLength.Short).Show();
});

var appBar = Activity.FindViewById<AppBarLayout>(Resource.Id.appbar);
if (appBar != null)
appBar.OffsetChanged += (sender, args) => swipeToRefresh.Enabled = args.VerticalOffset == 0;

return view;
}

public override void OnDestroyView()
{
base.OnDestroyView();
_itemSelectedToken.Dispose();
_itemSelectedToken = null;
}
}
}

这是我当前的 View ,我可以在其中选择和取消选择复选框,但我不知道如何处理此选择事件以删除所选项目。

enter image description here

更新:

仍然,我不知道如何处理选中的复选框以启用删除操作

class ListItem
{
public int Id { get; set; }
public string Text { get; set; }
private bool _isChecked;

public bool IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
}
}
}

public class MyViewModel:MvxViewModel
{

private ObservableCollection<MyListViewModel> _myListViews;

public ObservableCollection<MyListViewModel> MyListViews
{
get { return _myListViews; }
set
{
_myListViews= value;
RaisePropertyChanged(() => MyListViews);
}
}
public void Init(string myId)
{
List<ListItem> allListItems = new List<ListItem>();
allListItems = _myService.GetAllItems(myId);
foreach (var myTest in allListItems)
{
_myListViews.Add(MyListViewModel.CreateViewModel(myTest));
}
}
}

最佳答案

IsChecked 属性添加到 ListItem 类并将其绑定(bind)到 listitem_recyclerviewexample 内的复选框,如下所示:local:MvxBind="已检查 IsChecked”

如果您的类(class)看起来像这样,您应该能够找出哪些项目已选中和未选中。

class ListItem
{
public int Id { get; set; }
public string Text { get; set; }
private bool _isChecked;

public bool IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
}
}
}

关于c# - Xamarin 中 MVVMCross 模式中的 RecyclerView 复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36553677/

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