简体   繁体   English

如何使用Xamarin Android Mvvmcross创建Recyclerview?

[英]How to create a recyclerview using xamarin android mvvmcross?

I have followed the example from https://github.com/xamarin/monodroid-samples/tree/master/android5.0/RecyclerViewSample/RecyclerViewSample for displaying the data in recyclerview in xamarin android. 我已遵循https://github.com/xamarin/monodroid-samples/tree/master/android5.0/RecyclerViewSample/RecyclerViewSample中的示例在xamarin android中的recyclerview中显示数据。 It is working fine for me. 对我来说很好。

But as per our project requirement, i need to display a recyclerview using xamarin android mvvmcross. 但是根据我们的项目要求,我需要使用xamarin android mvvmcross显示一个recyclerview。 I have searched for examples but could not find a working one. 我已经搜索了示例,但找不到有效的示例。

Code: 码:

    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);
            }
        }

Now when i run the application, the recyclerview is displaying empty. 现在,当我运行该应用程序时,recyclerview显示为空。 Can you please let me know if I am missing anything while binding the data. 您能否让我知道绑定数据时是否丢失了任何内容。

First of all install the package MvvmCross.Droid.Support.V7.RecyclerView . 首先,安装软件包MvvmCross.Droid.Support.V7.RecyclerView

Here you have an example of a simple MvxRecyclerView : 在这里,您有一个简单的MvxRecyclerView的示例:

<mvvmcross.droid.support.v7.recyclerview.MvxRecyclerView
    ...
    local:MvxItemTemplate="@layout/item_template"
    local:MvxBind="ItemsSource Items; ItemClick ItemClickCommand; ItemLongClick ItemLongClickCommand" />

So Items is your ObservableCollection<T> of your ViewModel , ItemClickCommand is the command of your ViewModel that will be executed when you click on a row and ItemLongClick is the command of your ViewModel that will be executed when you do a long click on a row. 所以Items是你ObservableCollection<T>您的ViewModelItemClickCommand是你的命令ViewModel ,当你点击一个行和将要执行ItemLongClick是你的命令ViewModel ,当你在一个行做久点击,将被处决。

Let's say you have a public ObservableCollection<PersonItemViewModel> Items {get;set;} 假设您有一个public ObservableCollection<PersonItemViewModel> Items {get;set;}

And your PersonItemViewModel has the next properties: 您的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);
}

So your item_template.axml that will be renderized as each row of the MvxRecyclerView . 所以,你的item_template.axml将被renderized的的每一行MvxRecyclerView It's DataContext (to which you do the bindings) is each of the items of the ObservableCollection<PersonItemViewModel> , ie a PersonItemViewModel . 它是DataContext (您要绑定到它)是ObservableCollection<PersonItemViewModel>每个项目,即PersonItemViewModel Here an example of the layout: 这是布局的示例:

<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>

More info in Android MvxRecyclerView HIH Android MvxRecyclerView HIH中的更多信息

If you are using Mvvmcross 6. Put that code in your Setup.cs at your .Droid project: 如果使用的是Mvvmcross 6,则将该代码放在.Droid项目的Setup.cs中:

protected override IEnumerable<Assembly> AndroidViewAssemblies =>
       new List<Assembly>(base.AndroidViewAssemblies)
       {
            typeof(MvxRecyclerView).Assembly,
       };

Now you don't have to use the full path of MvxRecyclerView (MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView). 现在,您不必使用MvxRecyclerView的完整路径(MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView)。

Now you can use the MvxRecyclerView just like de RecyclerView from Android. 现在,您可以像使用Android中的RecyclerView一样使用MvxRecyclerView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM