Navigation Activity

Asked

Viewed 53 times

0

I would like some help when I open my Navigation Activity after login I get it like this: inserir a descrição da imagem aqui But I wish he was already called that with one of the pages marked like this: inserir a descrição da imagem aqui

activity_main_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/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>
  • You’re already carrying this fragment Consultas Abertas?

  • So, I’m novice in android, and put me to do this project, so I don’t know much how to use the navigation Activity @Leonardodias

1 answer

1


Your Activity, is implementing NavigationView.OnNavigationItemSelectedListener, right?

Then you will 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 verify which menu id was clicked, and will call the Fragment corresponding to it, and the system itself will take care of selecting 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 finally, in the XML of your Activity, you should have the Framelayout where the Fragments will be loaded, referring to this 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>
  • Leonardo, then I should create an xml layout for each of the menu items?.replace(R.id.container, ConsultaAbertasFragment, name) and that part will be the Activity xml container, the layout name, and that name is q ?

  • No, in the same XML menu you put all the items, you should already have it created there, the XML name is activity_main_drawer. Already the name is only a name pro Fragment, does not impact anything, for example "Fragment queries"

  • I updated the question and put the activity_main_drawer.xml and that’s all there is in her

  • 1

    That’s it, it’s already right, each item is a button there in the menu, at the time you compare the id, inside the onNavigationItemSelected, you use if (id == R.id.nav_abertas) { Ai changes the Fragment that will be placed in the FrameLayout

  • I’m entering your code from FragmentManager fragmentManager = getSupportFragmentManager();&#xA; fragmentManager.beginTransaction()&#xA; .replace(R.id.container, ConsultaAbertasFragment, name)&#xA; .commit(); but in the part that is written Consultaabertasfragment and in the part name do not know what to put (sorry to be layman in the subject)

  • In the Place of the Query Open Fragment, you have to put the name of the Fragment you created, each screen that opens through the menu there, is a different Fragment, and the name can put anything, for example: "open queries"

  • So the items that are created in activity_main_drawer.xml are Fragment? or are other files that are created ?

  • Back in the activity_main_drawer.xml are just the menu buttons, then each button will open a class that extends Fragment respective that button, for example, the R.id.nav_abertas will open the Fragment ConsultaAbertasFragment, already the R.id.nav_fechadas will open the Fragment ConsultasFechadasFragment. For each menu item, you have to have a class that extends Fragment

  • Thanks, it helped a lot guy sorry to take your time

  • Imagine, whenever you need us

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.