Your activity is implementing NavigationView.OnNavigationItemSelectedListener
, right?
So you have a method like this:
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.consultas_abertas) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, ConsultaAbertasFragment, name)
.commit();
}
//Resto do código
drawer.closeDrawer(GravityCompat.START);
return true;
}
The system will check which id of the menu has been clicked, and will call the Fragment corresponding to it, and the system itself will take care of selecting there in the menu the Fragment that was called.
The XML of your Menu should look something like this:
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/consultas_abertas"
android:title="Consultas Abertas" />
//Resto do código
</group>
</menu>
And lastly, in the XML of your Activity, you should have the FrameLayout where the Fragments will be loaded, referring to that id R.id.container
, example:
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>