OnOptionsItemSelected在Xamarin中不起作用

时间:2015-08-11 20:29:09

标签: c# android xamarin slidingdrawer onitemselectedlistener

我正在尝试使用Xamarin创建一个Android应用程序。

我有一个ActionBarActivity活动,左抽屉菜单里面有几个项目。但当我按下其中一个项目没有任何反应时,我在OnOptionsItemSelected函数内放置了一个断点,它并没有就此止步。这是我的MainActivity.cs文件:

namespace IBuy {
    [Activity(Label = "Main Screen", Theme="@style/MyTheme")]
    public class MainScreen: ActionBarActivity
    {
        private SupportToolBar mToolbar;
        private MyActionBarDrawerToggle mDrawerToggle;
        private DrawerLayout mDrawerLayout;
        private ListView mLeftDrawer;


        protected override void OnCreate(Bundle bundle) {
            base.OnCreate (bundle);
            SetContentView(Resource.Layout.MainScreen);

            mToolbar = FindViewById<SupportToolBar> (Resource.Id.toolbar);
            mDrawerLayout = FindViewById<DrawerLayout> (Resource.Id.drawer_layout);
            mLeftDrawer = FindViewById<ListView> (Resource.Id.left_drawer);
            SetSupportActionBar (mToolbar);
            String[] arr = Resources.GetStringArray(Resource.Array.myList);

            ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, arr);
            mLeftDrawer.Adapter = adapter;

            mDrawerToggle = new MyActionBarDrawerToggle (this, mDrawerLayout, Resource.String.openDrawer, Resource.String.closeDrawer);

            mDrawerLayout.SetDrawerListener (mDrawerToggle);

            SupportActionBar.SetHomeButtonEnabled (true);
            SupportActionBar.SetDisplayShowTitleEnabled (true);
            mDrawerToggle.SyncState ();
        }

        public override bool OnOptionsItemSelected (IMenuItem item)
        {
            Console.WriteLine ("ID = " + item.ItemId);
            mDrawerToggle.OnOptionsItemSelected (item);
            return base.OnOptionsItemSelected (item);
        }

    }
}

这是我的MainScreen.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            .
            .
            .
        </RelativeLayout>
        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="#818181"
            android:dividerHeight="1dp"
            android:background="#E3F2FD" />
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

这是我的MyActionBarDrawerToggle.cs文件:

using System;
using SupportActionBarDrawerToggle = Android.Support.V7.App.ActionBarDrawerToggle;
using Android.Support.V7.App;
using Android.Support.V4.Widget;

namespace IBuy
{
    public class MyActionBarDrawerToggle : SupportActionBarDrawerToggle
    {
        private ActionBarActivity mHostActivity;
        private int mOpenResource;
        private int mCloseResource;

        public MyActionBarDrawerToggle (ActionBarActivity host, DrawerLayout drawerLayout, int openedResource, int closedResource)
            : base(host,drawerLayout,openedResource,closedResource)
        {
            mHostActivity = host;
            mOpenResource = openedResource;
            mCloseResource = closedResource;
        }

        public override void OnDrawerOpened (Android.Views.View drawerView)
        {
            base.OnDrawerOpened (drawerView);
        }

        public override void OnDrawerClosed (Android.Views.View drawerView)
        {
            base.OnDrawerClosed (drawerView);
        }

        public override void OnDrawerSlide (Android.Views.View drawerView, float slideOffset)
        {
            base.OnDrawerSlide (drawerView, slideOffset);
        }

        public override bool OnOptionsItemSelected (Android.Views.IMenuItem item)
        {
            int num = item.ItemId;
            return base.OnOptionsItemSelected (item);
        }
    }
}

我做错了什么?有人可以帮我一把吗?

提前谢谢

2 个答案:

答案 0 :(得分:0)

不确定这个,但我认为你的问题在OnOptionsItemSelected

尝试将其更改为此

switch(item.ItemId)
{
case Resource.Id.<controlID>: 
// run some code;
break;
}

controlID是抽屉中声明的ID

答案 1 :(得分:0)

答案完全不同。我刚刚在ListView上处理了ItemClick事件,现在它可以正常工作。