簡體   English   中英

如何使用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是你ObservableCollection<T>您的ViewModelItemClickCommand是你的命令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將被renderized的的每一行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 HIH中的更多信息

如果使用的是Mvvmcross 6,則將該代碼放在.Droid項目的Setup.cs中:

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

現在,您不必使用MvxRecyclerView的完整路徑(MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView)。

現在,您可以像使用Android中的RecyclerView一樣使用MvxRecyclerView。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM