如何使用 xamarin android mvvmcross 创建 recyclerview?
How to create a recyclerview using xamarin android mvvmcross?
我按照 https://github.com/xamarin/monodroid-samples/tree/master/android5.0/RecyclerViewSample/RecyclerViewSample 中的示例在 xamarin android 中的 recyclerview 中显示数据。它对我来说很好用。
但是根据我们的项目要求,我需要使用 xamarin android mvvmcross 显示一个 recyclerview。我搜索了示例,但找不到有效的示例。
代码:
main.xml
<MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView
android:id="@+id/recylerView"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_marginLeft="@dimen/margin_normal"
android:layout_marginRight="@dimen/margin_normal"
android:layout_marginBottom="@dimen/margin_normal"
android:layout_marginTop="@dimen/margin_normal"
android:background="#FFFF00"
android:orientation="horizontal"
android:divider="@null"
android:dividerHeight="@dimen/divider_height_medium"
local:MvxItemTemplate="@layout/item_list"
local:MvxBind="ItemsSource CoffeeList" />
Fragment.cs
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.Inflate(Resource.Layout.main, null);
return v;
}
item_list.xml
<LinearLayout
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="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
local:MvxBind="Text FirstName" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
local:MvxBind="Text LastName" />
</LinearLayout>
ViewModel.cs
public class CoffeeViewModel : MvxViewModel
{
public CoffeeViewModel ()
{
}
private string _firstName;
public string FirstName
{
get => _firstName;
set => SetProperty(ref _firstName, value);
}
private string _lastName;
public string LastName
{
get => _lastName;
set => SetProperty(ref _lastName, value);
}
}
现在当我 运行 应用程序时,recyclerview 显示为空。如果我在绑定数据时遗漏了什么,请告诉我。
首先安装包MvvmCross.Droid.Support.V7.RecyclerView
。
这里有一个简单的例子 MvxRecyclerView
:
<mvvmcross.droid.support.v7.recyclerview.MvxRecyclerView
...
local:MvxItemTemplate="@layout/item_template"
local:MvxBind="ItemsSource Items; ItemClick ItemClickCommand; ItemLongClick ItemLongClickCommand" />
所以Items
是你ViewModel
的ObservableCollection<T>
,ItemClickCommand
是你ViewModel
的命令,当你点击一个行和 ItemLongClick
是您的 ViewModel
的命令,当您长按一行时将执行该命令。
假设您有 public ObservableCollection<PersonItemViewModel> Items {get;set;}
并且您的 PersonItemViewModel
具有以下属性:
private string _firstName;
public string FirstName
{
get => _firstName;
set => SetProperty(ref _firstName, value);
}
private string _lastName;
public string LastName
{
get => _lastName;
set => SetProperty(ref _lastName, value);
}
因此您的 item_template.axml
将呈现为 MvxRecyclerView
的每一行。 DataContext
(您对其进行绑定)是 ObservableCollection<PersonItemViewModel>
的每个项目,即 PersonItemViewModel
。这里的布局示例:
<LinearLayout
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="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
local:MvxBind="Text FirstName" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
local:MvxBind="Text LastName" />
</LinearLayout>
更多信息在Android MvxRecyclerView
嗨
如果您使用的是 Mvvmcross 6。将该代码放入您的 Setup.cs 中的 .Droid 项目中:
protected override IEnumerable<Assembly> AndroidViewAssemblies =>
new List<Assembly>(base.AndroidViewAssemblies)
{
typeof(MvxRecyclerView).Assembly,
};
现在您不必使用 MvxRecyclerView 的完整路径 (MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView).
现在您可以像 Android 中的 RecyclerView 一样使用 MvxRecyclerView。
我按照 https://github.com/xamarin/monodroid-samples/tree/master/android5.0/RecyclerViewSample/RecyclerViewSample 中的示例在 xamarin android 中的 recyclerview 中显示数据。它对我来说很好用。
但是根据我们的项目要求,我需要使用 xamarin android mvvmcross 显示一个 recyclerview。我搜索了示例,但找不到有效的示例。
代码:
main.xml
<MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView
android:id="@+id/recylerView"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_marginLeft="@dimen/margin_normal"
android:layout_marginRight="@dimen/margin_normal"
android:layout_marginBottom="@dimen/margin_normal"
android:layout_marginTop="@dimen/margin_normal"
android:background="#FFFF00"
android:orientation="horizontal"
android:divider="@null"
android:dividerHeight="@dimen/divider_height_medium"
local:MvxItemTemplate="@layout/item_list"
local:MvxBind="ItemsSource CoffeeList" />
Fragment.cs
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.Inflate(Resource.Layout.main, null);
return v;
}
item_list.xml
<LinearLayout
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="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
local:MvxBind="Text FirstName" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
local:MvxBind="Text LastName" />
</LinearLayout>
ViewModel.cs
public class CoffeeViewModel : MvxViewModel
{
public CoffeeViewModel ()
{
}
private string _firstName;
public string FirstName
{
get => _firstName;
set => SetProperty(ref _firstName, value);
}
private string _lastName;
public string LastName
{
get => _lastName;
set => SetProperty(ref _lastName, value);
}
}
现在当我 运行 应用程序时,recyclerview 显示为空。如果我在绑定数据时遗漏了什么,请告诉我。
首先安装包MvvmCross.Droid.Support.V7.RecyclerView
。
这里有一个简单的例子 MvxRecyclerView
:
<mvvmcross.droid.support.v7.recyclerview.MvxRecyclerView
...
local:MvxItemTemplate="@layout/item_template"
local:MvxBind="ItemsSource Items; ItemClick ItemClickCommand; ItemLongClick ItemLongClickCommand" />
所以Items
是你ViewModel
的ObservableCollection<T>
,ItemClickCommand
是你ViewModel
的命令,当你点击一个行和 ItemLongClick
是您的 ViewModel
的命令,当您长按一行时将执行该命令。
假设您有 public ObservableCollection<PersonItemViewModel> Items {get;set;}
并且您的 PersonItemViewModel
具有以下属性:
private string _firstName;
public string FirstName
{
get => _firstName;
set => SetProperty(ref _firstName, value);
}
private string _lastName;
public string LastName
{
get => _lastName;
set => SetProperty(ref _lastName, value);
}
因此您的 item_template.axml
将呈现为 MvxRecyclerView
的每一行。 DataContext
(您对其进行绑定)是 ObservableCollection<PersonItemViewModel>
的每个项目,即 PersonItemViewModel
。这里的布局示例:
<LinearLayout
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="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
local:MvxBind="Text FirstName" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
local:MvxBind="Text LastName" />
</LinearLayout>
更多信息在Android MvxRecyclerView 嗨
如果您使用的是 Mvvmcross 6。将该代码放入您的 Setup.cs 中的 .Droid 项目中:
protected override IEnumerable<Assembly> AndroidViewAssemblies =>
new List<Assembly>(base.AndroidViewAssemblies)
{
typeof(MvxRecyclerView).Assembly,
};
现在您不必使用 MvxRecyclerView 的完整路径 (MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView).
现在您可以像 Android 中的 RecyclerView 一样使用 MvxRecyclerView。