我正在尝试在导航抽屉菜单下实现SubMenu。只有一个组菜单。请参见下图我要做什么。目前,我仅从xml菜单列表中添加了主菜单。如何将ExpandableListView作为子菜单添加到菜单以获取以下类型的接口。
在这里,我用来打开/关闭导航抽屉并在抽屉菜单上启动活动的代码单击
navigationView = (NavigationView) findViewById(R.id.navigation_view);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem item) {
Intent intent;
int id = item.getItemId();
//Check to see which item was being clicked and perform appropriate action
if (id == R.id.favorites) {
Intent favoritesIntent = new Intent(MainActivity.this, Favorites.class);
startActivity(favoritesIntent);
} else if (id == R.id.settings) {
Intent settingsIntent = new Intent(MainActivity.this, Settings.class);
startActivity(settingsIntent);
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
});
// Initializing Drawer Layout and ActionBarToggle
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){
@Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
ImageView titleLogo = (ImageView) findViewById(R.id.titleLogo);
titleLogo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawerLayout.openDrawer(GravityCompat.START);
}
});
.....
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
这是我使用的XML
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:itemTextColor="@color/white"
app:itemIconTint="@color/white"
app:headerLayout="@layout/header"
app:menu="@menu/drawer"
android:background="@color/colorPrimary" >
</android.support.design.widget.NavigationView>
这是菜单列表的drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/favorites"
android:checked="false"
android:icon="@drawable/favorites"
android:title="@string/favorites" />
<item
android:id="@+id/services"
android:checked="false"
android:icon="@drawable/services"
android:title="@string/services" />
<item
android:id="@+id/settings"
android:checked="false"
android:icon="@drawable/settings"
android:title="@string/settings" />
</group>
</menu>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句