我的导航抽屉包含很少的菜单项。我正在尝试实现菜单项单击事件,但单击事件根本没有注册。
这是我的抽屉:
drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
// Create ActionBarDrawerToggle button and add it to the toolbar
var toolbar = FindViewById<V7Toolbar>(Resource.Id.toolbar);
// toolbar.InflateMenu(Resource.Menu.top_menus);
SetSupportActionBar(toolbar);
var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.drawer_open, Resource.String.drawer_close);
drawerLayout.SetDrawerListener(drawerToggle);
drawerToggle.SyncState();
navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
setupDrawerContent(navigationView);
这是setupDrawerContent
代码:_
void setupDrawerContent(NavigationView navigationView)
{
navigationView.NavigationItemSelected += (sender, e) =>
{
e.MenuItem.SetChecked(true);
drawerLayout.CloseDrawers();
};
}`
这是OnCreateOptionsMenu
代码:
public override bool OnCreateOptionsMenu(IMenu menu)
{
navigationView.InflateMenu(Resource.Menu.nav_menu); //Navigation Drawer Layout Menu Creation
//MenuInflater.Inflate(Resource.Menu.nav_menu, menu);
return base.OnCreateOptionsMenu(menu);
//return true;
}
这是OnOptionsItemSelected
代码:
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Resource.Id.nav_sangli:
return true;
case Resource.Id.nav_solapur:
return true;
default:
break;
}
return base.OnOptionsItemSelected(item);
}
问题是调试器甚至没有达到OnOptionsItemSelected
我尝试过xamarin论坛,但没有任何效果。 TIA
答案 0 :(得分:0)
您使用了错误的构造函数ActionBarDrawerToggle
https://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html
你正在使用这个:
ActionBarDrawerToggle (Activity activity,
DrawerLayout drawerLayout,
Toolbar toolbar,
int openDrawerContentDescRes,
int closeDrawerContentDescRes)
尝试使用这个:
ActionBarDrawerToggle (Activity activity,
DrawerLayout drawerLayout,
int openDrawerContentDescRes,
int closeDrawerContentDescRes)
所以这样定义抽屉:
var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, Resource.String.drawer_open, Resource.String.drawer_close);
将工具栏设置为:
SetSupportActionBar(toolbar);
通常会触发事件
更新: 别忘了添加:
navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected;
这是抽屉式参数: 改变这一行:
var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,Resource.String.drawer_open, Resource.String.drawer_close);
到:
`var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.drawer_open, Resource.String.drawer_close);`