Xamarin:让 MvxRecyclerView 工作

Xamarin: Get MvxRecyclerView working

我正在使用 Xamarin 和 MvvmCross 构建一个不错的 Android 应用程序。我正在尝试让 MvxRecyclerView 正常工作,但我做不好。应用 运行但未在 RecycleView.

中显示任何内容

这是我创建的视图 (MainView.axml):

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView
        android:id="@+id/rvItems"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        local:MvxItemTemplate="@layout/itemview"
        local:MvxBind="ItemsSource TestItems" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add" />
</LinearLayout>

我创建的 ViewModel (MainViewModel.cs):

public class MainViewModel : MvxViewModel
{
    #region Properties

    public ObservableCollection<TestModel> TestItems { get; set; }

    #endregion

    public MainViewModel()
    {
        TestItems = new ObservableCollection<TestModel>
        {
            new TestModel()
            {
                Name = "Test1"
            },
            new TestModel()
            {
                Name = "Test2"
            }
        };
    }

    public override Task Initialize()
    {
        //TODO: Add starting logic here

        return base.Initialize();
    }
}

项目视图(ItemView.cs):

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://scehmas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Name" />

正如您在示例中看到的,它很简单,但我无法让它工作。

Github link

这里有两个不同的问题:

  1. 你的ItemTemplate的宽度是0dp,所以没有渲染。将其更改为 match_parent。我看到您正在尝试使用 layout_weight,但要使父 LinearLayout 正常工作,需要 weightSum.

  2. 您的 TextView 未绑定到模型中 Name 的值。将 local:MvxBind="Text Name" 添加到 TextView.

进行这两项更改,您应该会看到您的 RecyclerView 您期望的方式: