credo, ho archiviato l'effetto desiderato:

L'idea è dietro a clear()
menu esistente e ri- inflateMenu
ogni volta che l'utente clicca sulla voce di menu, che dovrebbe portare a un sottomenu. (E su di tornare - fare praticamente lo stesso)
Come un menu principale (activity_main_drawer.xml
), ho usato questo:
<?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/action_item1"
android:icon="@drawable/ic_menu_camera"
android:title="Item 1" />
<item
android:id="@+id/action_expand"
android:icon="@drawable/ic_menu_gallery"
android:title="Sub Menu to expand" />
</group>
</menu>
Questo è un sottomenu():
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Go Back"
android:icon="@drawable/ic_back"
android:id="@+id/back_to_main"/>
<item
android:id="@+id/sub_item1"
android:icon="@drawable/ic_menu_share"
android:title="Subitem 1" />
<item
android:id="@+id/sub_item2"
android:icon="@drawable/ic_menu_send"
android:title="Subitem 2" />
</menu>
MainActivity.xml
:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main" />
</android.support.v4.widget.DrawerLayout>
Ed ecco la MainActivity
Classe (che fanno la commutazione):
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
.....
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.inflateMenu(R.menu.activity_main_drawer);
navigationView.setNavigationItemSelectedListener(this);
}
....
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.action_expand) {
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_submenu_drawer);
} else if (id == R.id.back_to_main) {
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer);
}
return true;
}
}
ho caricato la mia prova-progetto al mio set - feel free to check it out
Spero, aiuta
fonte
2015-12-26 13:25:38
Hai qualche soluzione, anche io sto provando questo, Se hai fatto, per favore condividilo una volta, Avanzato Grazie per la tua risposta .. –