【发布时间】:2015-11-04 05:53:30
【问题描述】:
我正在尝试使用 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);
}
}
}
我做错了什么?有人可以帮帮我吗?
提前谢谢你
【问题讨论】:
标签: c# android xamarin slidingdrawer onitemselectedlistener