Navigation Activity

0

I would like some help when I open my Navigation Activity after I log in so: ButIwishitwasalreadycalledwithoneofthepagesmarkedlikethis:

activity_main_drawer.xml

<?xmlversion="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_abertas"
            android:title="Consultas Abertas"
            android:visible="true" />
        <item
            android:id="@+id/nav_fechadas"
            android:title="Consultas Fechadas" />
        <item
            android:id="@+id/nav_medicos"
            android:title="Médicos" />
    </group>

</menu>
    
asked by anonymous 14.07.2017 / 15:35

1 answer

1

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>
    
14.07.2017 / 16:10