2014-04-25 11 views
3

我目前正在實施一個跨平臺的應用程序內Android和iOS。NavigationDrawer AddFooter方法MvxListView - 的NullReferenceException

因爲我想用MvvmCross和彈出導航,如果發現本傑明Hysell的演示(使用DrawerLayout)非常有幫助的:

代碼: https://github.com/benhysell/V.FlyoutTest

說明: http://benjaminhysell.com/archive/2014/04/mvvmcross-flyoutnavigation-hamburger-menu-sliding-menu-for-android-and-ios/

基礎的在此代碼中,我想在彈出導航中的菜單下添加一些其他控件。

所以在HomeView,我的方法

protected override void OnCreate(Bundle savedInstanceState) 
{ 
    // .... 

    var layout = this.BindingInflate(Resource.Layout.DrawerFooterView, null); 
    drawerList.AddFooterView(layout); 
} 

的末尾添加以下代碼現在,當我啓動應用程序,我可以看到寄託都蠻好,包括在彈出菜單我的額外控制。但是,一旦我點擊菜單項,我MvvmCross得到一個NullReferenceException:

04-25 17:42:20.892 I/MonoDroid(32443): UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object 
04-25 17:42:20.892 I/MonoDroid(32443): at Cirrious.MvvmCross.Binding.Droid.Views.MvxListView.ExecuteCommandOnItem (System.Windows.Input.ICommand,int) <IL 0x0000b, 0x000a0> 
04-25 17:42:20.892 I/MonoDroid(32443): at Cirrious.MvvmCross.Binding.Droid.Views.MvxListView.<EnsureItemClickOverloaded>b__0 (object,Android.Widget.AdapterView/ItemClickEventArgs) <IL 0x0000d, 0x000a7> 
04-25 17:42:20.892 I/MonoDroid(32443): at Android.Widget.AdapterView/IOnItemClickListenerImplementor.OnItemClick (Android.Widget.AdapterView,Android.Views.View,int,long) [0x0000d] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/a1e3982a/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.Widget.AdapterView.cs:261 
04-25 17:42:20.892 I/MonoDroid(32443): at (wrapper dynamic-method) object.289e379c-ed35-42d0-8505-cc91a6c90d7b (intptr,intptr,intptr,intptr,int,long) <IL 0x00029, 0x0009b> 

任何人都可以給這個問題提供幫助,因爲我的想法。

任何幫助,將不勝感激。

+0

什麼是您的佈局和視圖模型是什麼樣子? – Kiliman

+0

@Kiliman,'DrawerFooterView'的佈局是一個帶有幾個TextViews的簡單的LinearLayout,'DrawerFooterViewModel'是空的,因爲這些TextViews都沒有綁定到任何屬性。 – gridr

+0

可能鏈接到https://github.com/MvvmCross/MvvmCross/issues/602 – Stuart

回答

4

我不明白爲什麼增加一個HeaderFooter視圖到ListView是如此重要。

肯定有一些怪癖直接將其添加到MvxListView,但你可以做自己的工作用很少的努力。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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"> 

    <LinearLayout 
     android:id="@+id/header" 
     android:orientation="vertical" 
     android:layout_alignParentTop="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <!-- Header content here --> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/footer" 
     android:orientation="vertical" 
     android:layout_alignParentBottom="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <!-- Footer content here --> 
    </LinearLayout> 

    <MvxListView 
     android:id="@+id/listview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/header" 
     android:layout_above="@+id/footer" 
     local:MvxBind="ItemsSource Items; ItemSelected SelectedItem"/> 
</RelativeLayout> 

然後你就可以添加EventHandler,如果你想顯示/隱藏頁腳是否已見底的Scroll事件:

var footer = FindViewById<LinearLayout>(Resource.Id.footer); 
var lv = FindViewById<MvxListView>(Resource.Id.listview); 
lv.Scroll += (s, e) => 
{ 
    var lastItem = e.FirstVisibleItem + e.VisibleItemCount; 
    if(lastItem == e.TotalItemCount) 
    { 
     // we are at the end of the list 
     // maybe do some animation before showing it 
     footer.Visibility = ViewStates.Visible; 
    } 
    else 
    { 
     footer.Visibility = ViewStates.Gone; 
    } 
}; 
+0

是的,它會工作,但顯示/隱藏動畫將不同於列表視圖滾動。 –

+0

感謝@Cheesebaron爲您解答。最後,我實現了類似於此的東西,儘管沒有可見性部分。 我只是想,使用列表的頁腳將是一個不錯的和方便的解決方案。 – gridr

+0

從長遠來看,我想這個MvxAdapter的問題應該以某種方式解決。我認爲已經有一些建議的解決方案[#602](https://github.com/MvvmCross/MvvmCross/issues/602) – Cheesebaron

0

暫時不支持MvxListView AddHeader/AddFooter方法。

在極端情況下,你可以爲你導航列表視圖添加自定義項目: Polymorphic Lists

因此,你將有兩個類型的項目:

1 - 默認導航排

2 - 頁腳,在你的情況下