私は以下を持っています。 axmls
は基本的にはrecyclerview
で、テキストビューとチェックボックスが含まれています。 mvvmcross
デザインで選択したアイテムを削除するためのチェックボックスと対話する方法を理解できませんでした。RecyclerView Xamarinの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
ここでは、チェックボックスを選択したり解除したりすることができる現在のビューですが、選択したアイテムを削除するためにこのselectイベントを処理する方法はわかりません。
更新:
はそれでも、私はListItem
クラスに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;
}
}
}
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));
}
}
}
はい、1時間前に追加したものです。しかし、どの項目がチェックされているかはどのように分かりますか? – hotspring
@texas:ListItemにIdプロパティを追加すると、SetCheckedのセッターでそのプロパティを読み取ることができるはずです。 – Giorgi
気にしない場合、簡単な例を教えてください。 – hotspring