2017-01-09 74 views
0

在MvvmCross中,我有一個可以工作的活動中的android listview。如何在MvvmCross中使用MvxRecyclerView實現Recycler視圖

我在某處閱讀將列表視圖更改爲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(); 
     } 
    } 
} 
+0

嘗試用MvxAppCompatActivity替換MvxActivity。 – sJy

+0

請顯示您的Setup.cs文件。我敢打賭你還沒有在那裏註冊。 – Cheesebaron

+0

@sJy我使用MvxAppCompatActivity得到相同的錯誤。 –

回答

2

OK,我RecyclerView與MvxRecyclerView更換MvxListView後輕鬆地工作。不知道你的實現有什麼特別的問題,但我會給你我的花絮,你可以嘗試一切。

我的RecyclerView住在繼承自MvxFragment的片段中 - 與您的有點不同,但不應該是決定性因素。下面是一些片段摘錄:

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

我確實看到有點差異的在你的視圖模型的實現 - 我的是這樣的:

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

希望這有助於。

相關問題