我有index.php
,它使用recyclerview将所有人显示为列表。每个人都有年龄,性别,姓名属性以及复选框以便删除它。
我无法弄清楚如何在MainViewModel
中捕获用户复选框事件?
MainView.axml
MainViewModel
PersonTemplate.axml
<CheckBox
android:id="@+id/checkbox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
local:MvxBind="Checked IsAllSelected" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Name"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Age" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Gender" />
</LinearLayout>
<MvxRecyclerView
android:id="@+id/personRecyclerView"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
local:MvxItemTemplate="@layout/persontemplate"
local:MvxBind="ItemsSource Items; ItemClick ItemSelected" />
</LinearLayout>
我正在尝试根据每个项目的<?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="match_parent"
android:gravity="center_vertical"
android:layout_height="60dp">
<CheckBox
android:id="@+id/chked"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
local:MvxBind="Checked IsSelected; Click CheckBoxSelectionCommand;" />
<TextView
android:id="@+id/Name"
android:layout_width="0dp"
android:layout_height="wrap_content"
local:MvxBind="Text Name"
android:layout_weight="1" />
<TextView
android:id="@+id/Age"
android:layout_width="0dp"
android:layout_height="wrap_content"
local:MvxBind="Text Age"
android:layout_weight="1" />
<TextView
android:id="@+id/Gender"
android:layout_width="0dp"
android:layout_height="wrap_content"
local:MvxBind="Text Gender
android:layout_weight="1" />
</LinearLayout>
选项更新IsDeleteBtnShow
。
MainViewModel.cs
checkbox
我有以下viewmodel,public bool IsAllSelected
{
get { return _isAllSelected; }
set
{
_isAllSelected= value;
Items.ForEach(x => x.IsSelected = _isAllSelected);
IsDeleteBtnShow = _isAllSelected;
RaisePropertyChanged(() => IsAllSelected);
}
}
用来将人员列表为列表。
PersonRecyclerViewModel.cs
RecyclerView
答案 0 :(得分:2)
您可以将回调或命令传递给ItemViewModel的cration。 CreateViewModel(Person person, Action<PersonViewModel> checkboxSelectedCallback)
并将其直接用作CheckBoxSelectionCommand
或public ICommand CheckBoxSelectionCommand
{
get
{
return new MvxCommand(() =>
{
var isChecked = IsSelected;
ParentCheckBoxSelectionCallback(this);
});
}
}
。像这样:
<强> CheckBoxSelectionCommand 强>
public static PersonViewModel CreateViewModel(Person person, Action<PersonViewModel> checkboxSelectedCallback)
{
return new PersonViewModel
{
IsSelected = entity.IsSelected,
Age = entity.Age,
ParentCheckBoxSelectionCallback = checkboxSelectedCallback,
Gender= entity.Gender,
Name= entity.Name,
};
}
<强> CreateViewModel 强>
// only create once.
_checkedChangedCallback = (person =>
{
// do what you have to do if a item got selected
});
// where you create persons
CreateViewModel(person, _checkedChangedCallback );
<强> MainViewModel 强>
usort
答案 1 :(得分:1)
如果不在模型中添加操作或任何逻辑,另一种方法是使模型实现 INotifyPropertyChanged :
class PersonRecyclerViewModel : INotifyPropertyChanged
最快捷的方法是使用Fody PropertyChanged,因为只需在模型中添加属性即可获得界面的整个实现:
[ImplementPropertyChanged]
class PersonRecyclerViewModel {}
在ViewModel中,当您获取或刷新数据源时,for循环将侦听项属性更改:
foreach(var item in Items)
{
var n = (INotifyPropertyChanged)item;
n.PropertyChanged += OnItemPropertyChanged;
}
private void OnItemPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
{
if(propertyChangedEventArgs.PropertyName == "IsSelected")
{
// do whatever you need here
}
}