【问题标题】:How to implement Recycler View in MvvmCross using MvxRecyclerView如何使用 MvxRecyclerView 在 MvvmCross 中实现 Recycler View
【发布时间】:2023-04-02 11:57:01
【问题描述】:

在 MvvmCross 中,我在一个有效的活动中有一个 android 列表视图。

我在某处读到,将 listView 更改为 recyclerView 就像在我的布局 .axml 文件中将 MvxListView 更改为 MvxRecyclerView 一样简单。尝试这样做会给我以下运行时异常:

Android.Views.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class Mvx.MvxRecyclerView

在使用 MvxRecyclerView 时,我必须在后面的代码或视图模型中做些什么不同的事情吗?下面是我的代码。

布局文件:

Main.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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Mvx.MvxRecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        android:id="@+id/words_listview"
        local:MvxItemTemplate="@layout/words_listview_row"
        local:MvxBind="ItemsSource Words" />
</LinearLayout> 

words_listview_row.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    p1:minWidth="25px"
    p1:minHeight="25px"
    p1:layout_width="match_parent"
    p1:layout_height="match_parent"
    p1:id="@+id/relativeLayout1"
    p1:background="#FFFFFF">
    <TextView
        p1:text="Word name"
        p1:layout_width="wrap_content"
        p1:layout_height="wrap_content"
        p1:id="@+id/headingTextView"
        p1:width="325dp"
        p1:textColor="#000000"
        p1:layout_alignParentLeft="true"
        p1:textSize="18sp"
        p1:paddingLeft="20dp"
        p1:paddingTop="15dp"
        local:MvxBind="Text Name" />
    <TextView
        p1:text="Word meaning"
        p1:layout_width="match_parent"
        p1:layout_height="wrap_content"
        p1:layout_below="@id/headingTextView"
        p1:id="@+id/detailTextView"
        p1:textColor="#8f949a"
        p1:layout_alignParentLeft="true"
        p1:textSize="16sp"
        p1:paddingLeft="20dp"
        p1:paddingRight="20dp"
        p1:paddingBottom="15dp"
        local:MvxBind="Text Meaning" />
</RelativeLayout>

WordViewModel.cs

using MvvmCross.Core.ViewModels;
using VocabBuilder.Core.Models;
using VocabBuilder.Core.Services.Interfaces;

namespace VocabBuilder.Core.ViewModels
{
    public class WordViewModel : MvxViewModel
    {
        private IWordService _wordService;

        public MvxObservableCollection<Word> Words { get; set; }

        public WordViewModel(IWordService wordService)
        {
            Words = new MvxObservableCollection<Word>();

            _wordService = wordService;

            Words.ReplaceWith(_wordService.GetAllWords());
        }
    }
}

代码隐藏/WordView.cs

using Android.App;
using Android.OS;
using MvvmCross.Droid.Views;
using VocabBuilder.Core.ViewModels;

namespace VocabBuilder.UI.Droid.Views
{
    [Activity(Label="Vocab Builder", MainLauncher=true)]
    public class WordView : MvxActivity<WordViewModel>
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);
        }
    }
}

谢谢。

编辑:

这是我的 Setup.cs 文件:

using Android.Content;
using MvvmCross.Core.ViewModels;
using MvvmCross.Droid.Platform;

namespace VocabBuilder.UI.Droid
{
    public class Setup : MvxAndroidSetup
    {
        public Setup(Context applicationContext) : base(applicationContext)
        {
        }

        protected override IMvxApplication CreateApp()
        {
            return new Core.App();
        }
    }
}

【问题讨论】:

  • 尝试用 MvxAppCompatActivity 替换 MvxActivity。
  • 请出示您的 Setup.cs 文件。我敢打赌你还没有在那里注册。
  • @sJy 我在使用 MvxAppCompatActivity 时遇到了同样的错误。
  • @Cheesebaron 我在底部添加了 Setup.cs 文件。我是否必须为回收站视图添加特定的注册码?
  • @ShaikatHaque - 如果您想使用速记,请在此处查看 Cheesebaron 的答案:stackoverflow.com/a/39913945/2754727

标签: xamarin data-binding xamarin.android mvvmcross


【解决方案1】:

好的,在用 MvxRecyclerView 替换 MvxListView 后,我让 RecyclerView 可以轻松工作。不知道你的实现的具体问题是什么,但我会给你我的花絮,你可以尝试一下。

我的 RecyclerView 位于从 MvxFragment 继承的 Fragment 中 - 与您的有点不同,但不应成为决定因素。以下是一些 Fragment sn-ps:

public class ListsFragment : MvxFragment<ListsViewModel>
{
   ...
   public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
   {
      base.OnCreateView(inflater, container, savedInstanceState);
      var view =  this.BindingInflate(Resource.Layout.fragment_listsfragment, null);   
      return view;
   }
   ...
}

这是 fragment_listsfragment 的 axml:

<?xml version="1.0" encoding="utf-8"?>
<MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    app:MvxItemTemplate="@layout/item_list"
    app:MvxBind="ItemsSource Lists; ItemClick ShowListItemsCommand" />

我确实看到您的 ViewModel 实现有一点不同 - 我的情况是这样的:

public class ListsViewModel : BaseViewModel
{
  ...
  private readonly IListService _listService;
  private ObservableCollection<Models.List> _lists;

  public ObservableCollection<Models.List> Lists
  {
    get { return _lists; }
    set
    {
      _lists = value;
      RaisePropertyChanged(() => Lists);
    }
  }

  public MvxCommand<Models.List> ShowListItemsCommand
  {
    get
    {
      return new MvxCommand<Models.List>(selectedList =>
      {
        ShowViewModel<ListItemsViewModel>
                (new { listId = selectedList.Id });
      });
    }
  }
  ...
  public override async void Start()
  {
    base.Start();
    await InitializeAsync();
  }

  protected override async Task InitializeAsync()
  {
    await _listService.InitializeAsync();
    Lists = (await _listService.GetListsAsync())
            .ToObservableCollection();
  }
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-14
    • 2021-10-18
    • 2021-12-26
    • 2019-11-11
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多