其他分享
首页 > 其他分享> > android-MvxListView中的NavigationDrawer AddFooter方法-NullReferenceException

android-MvxListView中的NavigationDrawer AddFooter方法-NullReferenceException

作者:互联网

我目前正在为Android和iOS实现一个crossplatform-app.

由于我想使用MvvmCross和弹出菜单,因此如果发现本杰明·海塞尔(Benjamin 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>

任何人都可以为这个问题提供帮助,因为我没有主意.

任何帮助,将不胜感激.

解决方法:

我不明白为什么将Header或Footer视图添加到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;
    }
};

标签:xamarin,mvvmcross,android,drawerlayout
来源: https://codeday.me/bug/20191029/1961358.html