问题描述
我有以下。 axmls
这基本上是一个 recyclerview
,其中包含一个TextView和复选框。我可以不能够弄清楚如何使用复选框互动,删除 mvvmcross
设计选定的项目。
fragment_example_recyclerview.axml
<?XML版本=1.0编码=UTF-8&GT?;
< android.support.design.widget.CoordinatorLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
XMLNS:地方=http://schemas.android.com/apk/res-auto
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
<包括
布局=@布局/ toolbar_actionbar/>
< MvxRecyclerView
机器人:ID =@ + ID / my_recycler_view
机器人:滚动条=垂直
机器人:layout_width =match_parent
机器人:layout_height =match_parent
本地:MvxItemTemplate =@布局/ listitem_recyclerviewexample
本地:MvxBind =ItemsSource的项目;项目单击ItemSelected/>
< /android.support.design.widget.CoordinatorLayout>
listitem_recyclerviewexample.xml
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
XMLNS:地方=http://schemas.android.com/apk/res-auto
机器人:方向=横向
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT><复选框机器人:ID =@ + ID / checkbox_meat
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT/><的TextView
机器人:ID =@ + ID / innerText属性
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginRight =5DP
机器人:填充=10dp
本地:MvxBind =TEXT标题/>
ExampleViewPagerViewModel.cs
使用MvvmCross.Core.ViewModels;命名空间Example.Core.ViewModels
{
公共类ExampleViewPagerViewModel
:MvxViewModel
{
公共RecyclerViewModel回收站{搞定;私人集; } 公共ExampleViewPagerViewModel()
{
回收=新RecyclerViewModel();
}
}
}
RecyclerViewModel.cs
使用MvvmCross.Core.ViewModels;
使用系统;
使用System.Collections.ObjectModel;
使用System.Threading.Tasks;
使用System.Windows.Input;命名空间Example.Core.ViewModels
{ 公共类RecyclerViewModel
:MvxViewModel
{
私人列表项_selectedItem; 公共RecyclerViewModel()
{
项目=新的ObservableCollection<&列表项GT; {
新的ListItem {标题=A},
新的ListItem {标题=B},
新的ListItem {标题=C},
新的ListItem {标题=D},
新的ListItem {标题=E}
};
} 私人的ObservableCollection<&列表项GT; _items; 公众的ObservableCollection<&列表项GT;项目
{
{返回_items; }
组
{
_items =价值;
RaisePropertyChanged(()=>产品);
}
} 公共列表项的SelectedItem
{
{返回_selectedItem; }
组
{
_selectedItem =价值;
RaisePropertyChanged(()=>的SelectedItem);
}
} 公共虚拟的ICommand ItemSelected
{
得到
{
返回新MvxCommand<&列表项GT;(项目=>
{
的SelectedItem =项目;
});
}
}
}
}
ExampleViewPagerFragment.cs
使用System.Collections.Generic;
使用Android.OS;
使用Android.Runtime;
使用Android.Support.Design.Widget;
使用Android.Support.V4.View;
使用Android.Views;
使用Example.Core.ViewModels;
使用MvvmCross.Droid.Support.V4;
使用MvvmCross.Droid.Support.V7.Fragging.Attributes;命名空间Example.Droid.Fragments
{
[MvxFragment(typeof运算(MainViewModel),Resource.Id.content_frame)
[寄存器(example.droid.fragments.ExampleViewPagerFragment)]
公共类ExampleViewPagerFragment:BaseFragment< ExampleViewPagerViewModel>
{
保护覆盖INT FragmentId => Resource.Layout.fragment_example_viewpager; 公众覆盖查看OnCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState)
{
VAR视图= base.OnCreateView(充气器,容器,savedInstanceState); VAR viewPager = view.FindViewById< ViewPager>(Resource.Id.viewpager);
如果(viewPager!= NULL)
{ VAR VM =新RecyclerViewModel();
VAR碎片=新的List< MvxFragmentPagerAdapter.FragmentInfo>();
的for(int i = 0; I< vm.Items.Count;我++)
{
fragments.Add(新MvxFragmentPagerAdapter.FragmentInfo(RecyclerView+(I + 1)的ToString()的typeof(RecyclerViewFragment)的typeof(RecyclerViewModel)));
} viewPager.Adapter =新MvxFragmentPagerAdapter(活动,ChildFragmentManager,片段);
} VAR tabLayout = view.FindViewById< TabLayout>(Resource.Id.tabs);
tabLayout.SetupWithViewPager(viewPager); 返回视图。
}
}
}
RecyclerViewFragment.cs
使用系统;
使用Android.OS;
使用Android.Runtime;
使用Android.Support.Design.Widget;
使用Android.Support.V7.Widget;
使用Android.Views;
使用Android.Widget;
使用MvvmCross.Platform.WeakSubscription;
使用MvvmCross.Binding.Droid.BindingContext;
使用MvvmCross.Droid.Support.V7.Fragging.Fragments;
使用MvvmCross.Droid.Support.V7.RecyclerView;
使用MvvmCross.Droid.Support.V4;
使用Example.Core.ViewModels;
使用MvvmCross.Droid.Support.V7.Fragging.Attributes;命名空间Example.Droid.Fragments
{
[MvxFragment(typeof运算(MainViewModel),Resource.Id.content_frame)
[寄存器(example.droid.fragments.RecyclerViewFragment)]
公共类RecyclerViewFragment:MvxFragment< RecyclerViewModel>
{
私人IDisposable的_itemSelectedToken; 公众覆盖查看OnCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState)
{
VAR忽略= base.OnCreateView(充气器,容器,savedInstanceState); VAR视图= this.BindingInflate(Resource.Layout.fragment_recyclerview,NULL); VAR recyclerView = view.FindViewById< MvxRecyclerView>(Resource.Id.my_recycler_view);
如果(recyclerView!= NULL)
{
recyclerView.HasFixedSize =真;
VAR的layoutManager =新LinearLayoutManager(活动);
recyclerView.SetLayoutManager(的layoutManager);
} _itemSelectedToken = ViewModel.WeakSubscribe(()=> ViewModel.SelectedItem,
(发件人,参数)=> {
如果(ViewModel.SelectedItem!= NULL)
Toast.MakeText(活动,
$选择:{} ViewModel.SelectedItem.Title
ToastLength.Short).Show();
}); VAR appBar = Activity.FindViewById< AppBarLayout>(Resource.Id.appbar);
如果(appBar!= NULL)
appBar.OffsetChanged + =(发件人,参数)=> swipeToRefresh.Enabled = args.VerticalOffset == 0; 返回视图。
} 公共覆盖无效OnDestroyView()
{
base.OnDestroyView();
_itemSelectedToken.Dispose();
_itemSelectedToken = NULL;
}
}
}
下面是我目前看来,在那里我可以选择并取消该复选框,但我不知道怎么才能删除所选项目处理这个select事件。
更新:
不过,我不明白如何处理选中的复选框,以便删除操作。
类的ListItem
{
公众诠释标识{搞定;组; }
公共字符串文本{搞定;组; }
私人布尔_isChecked; 公共BOOL器isChecked
{
{返回_isChecked; }
组
{
_isChecked =价值;
}
}
} 公共类MyViewModel:MvxViewModel
{ 私人的ObservableCollection< MyListViewModel> _myListViews; 公众的ObservableCollection< MyListViewModel> MyListViews
{
{返回_myListViews; }
组
{
_myListViews =价值;
RaisePropertyChanged(()=> MyListViews);
}
}
公共无效初始化(字符串MYID)
{
清单<&列表项GT; allListItems =新的List<&列表项GT;();
allListItems = _myService.GetAllItems(身份识别码);
的foreach(在allListItems VAR MYTEST)
{
_myListViews.Add(MyListViewModel.CreateViewModel(MYTEST));
}
}
}
添加器isChecked
属性设置为列表项
类并将其绑定到里面 listitem_recyclerviewexample
的复选框,这样的:地方:MvxBind =经过器isChecked
如果您的类看起来是这样的,你应该能够找出哪些项目检查,并选中。
类的ListItem
{
公众诠释标识{搞定;组; }
公共字符串文本{搞定;组; }
私人布尔_isChecked; 公共BOOL器isChecked
{
{返回_isChecked; }
组
{
_isChecked =价值;
}
}
}
I have the following .axmls
which is basically a recyclerview
which contains a textview and checkbox. I could not able to figure out how to interact with checkbox to delete the selected item in mvvmcross
design.
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;
}
}
}
Here is my current view, where I could select and unselect the checkbox, but I do not know how to handle this select event in order to delete the selected item.
Update:
Still, I dont get how to handle selected checkbox to enable delete operation
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));
}
}
}
Add IsChecked
property to the ListItem
class and bind it to the checkbox inside listitem_recyclerviewexample
like this: local:MvxBind="Checked IsChecked"
If your class looks like this you should be able to find out which items are checked and unchecked.
class ListItem
{
public int Id { get; set; }
public string Text { get; set; }
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
}
}
}
这篇关于RecyclerView复选框在Xamarin MVVMCross模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!