我有以下内容。axmls
基本上是recyclerview
,其中包含textview和复选框。我无法弄清楚如何与复选框进行互动以删除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;
}
}
}
这是我当前的视图,我可以在其中选择和取消选中该复选框,但我不知道如何处理此选择事件以删除所选项目。
更新
仍然,我不知道如何处理选中的复选框以启用删除操作
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));
}
}
}
答案 0 :(得分:4)
将IsChecked
属性添加到ListItem
类并将其绑定到listitem_recyclerviewexample
内的复选框,如下所示:local:MvxBind="Checked IsChecked"
如果您的课程看起来像这样,您应该能够找出检查和取消选中的项目。
class ListItem
{
public int Id { get; set; }
public string Text { get; set; }
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
}
}
}
答案 1 :(得分:0)
据我了解,您希望在选择要删除的项目后,可以删除ViewModel中的列表吗?
首先,我会在您的ViewModel中创建一个列表,您可以在其中存储要删除的ListItem。
private List<ListItem> _listItemsToDelete;
public List<ListItem> ListItemsToDelete
{
get
{
return _listItemsToDelete;
}
set
{
_listItemsToDelete= value;
RaisePropertyChanged(() => ListItemsToDelete);
}
}
还在ListItem中添加对Viewmodel的引用。既然您的项目中有ViewModel,您可以添加到之前添加的列表,如果未选中则删除。我还会正确地创建一个ListItemWrapper,它获取ListItem,IsChecked属性和ViewModel的实例,而不是将其混合到您的模型中。
class ListItem
{
public int Id { get; set; }
public string Text { get; set; }
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
if (IsChecked)
_parent.ListItemsToDelete.Add(Item);
else
_parent.ListItemsToDelete.Remove(Item);
RaisePropertyChanged(() => IsChecked);
}
}
readonly MyListViewModel _parent;
public ListItem(MyListViewModel parent)
{
_parent = parent;
}
}
现在,您只需在ViewModel中添加一个DeleteCommand属性,即删除“ListItemsToDelete”列表中的所有项目。您可以将此命令绑定到菜单项或FAB。
private MvxCommand _deleteCommand;
public IMvxCommand DeleteCommand
{
get
{
_deleteCommand = _deleteCommand ?? new MvxCommand(() =>
WebServiceDeleteStuff(ListItemsToDelete);
RemoveFromListItemsList(ListItemsToDelete);
ListItemsToDelete.Clear();
});
return _deleteCommand;
}
}
不确定这是完全“按书”,但它是如何解决这个问题的。希望这会有所帮助:)请记住,你仍然需要做Giorgi建议将复选框绑定到IsChecked。