How to call Fragments in Drawernavigation?

Asked

Viewed 734 times

3

I created a new project in android studio and chose to use the navigationDrawer, created my screens now I would like to call them. How do I implement this method and switch windows in menu of an application using the:

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    Fragment fragment = null;
    if (id == R.id.servic) {
       fragment = new Cronograma();
    } else if (id == R.id.nav_camera)  {
        fragment = new Localizacao();
    } else if (id == R.id.nav_gallery) {
        fragment = new Sobre_aplicativo();
    } else if (id == R.id.nav_share) {
        finish();
    }

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.mainFrame, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

ACTIVITY MAIN

<?xml version="1.0" encoding="utf-8"?>

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

    <FrameLayout
        android:id="@+id/flContent"
        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"
    app:menu="@menu/activity_main_drawer" />

CONTENT MAIN:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
    android:id="@+id/mainFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></FrameLayout>

<ListView
    android:id="@+id/listView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true" />

APP BAR MAIN:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

ACTIVITY MAIN DRAWER

<?xml version="1.0" encoding="utf-8"?>

<group android:checkableBehavior="single">
    <item
        android:id="@+id/servic"
        android:icon="@drawable/servico"
        android:title="Serviços" />
    <item
        android:id="@+id/nav_camera"
        android:icon="@drawable/pref"
        android:title="A prefeitura" />
    <item
        android:id="@+id/nav_gallery"
        android:icon="@drawable/local"
        android:title="Localização" />

</group>

<item android:title="Informações">
    <menu>

        <item
            android:id="@+id/nav_share"
            android:icon="@drawable/dev"
            android:title="Sobre o aplicativo" />
        <item
            android:id="@+id/nav_manage"
            android:icon="@drawable/exit"
            android:title="Sair" />
    </menu>
</item>

1 answer

3


The ideal is that the layout of your Activity has as one of its elements something that serves as "container", it is very common to use a Framelayout for these cases.

So create an Activity method that does the following:

String backStateName = fragment.getClass().getName();
        FragmentManager manager = getSupportFragmentManager();
        manager.popBackStackImmediate(backStateName, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(container (id do container), fragment);
        transaction.addToBackStack(backStateName);
        transaction.commit();

This method should receive as parameter an instance of the fragment you want to open, e.g.:

  if (id == R.id.nav_camera) {
       abreFragmento(new CameraFragment());
  }

Thus, your Activity fragmentManager will replace what is in the container with the Fragment that you instanced.

Extra:

In this method shown, the fragmentManager stores the Fragments that were opened in order, if you use abreFragment(new Outrofragment()) and another Fragment is already open, the same goes into onStop() and you can return to it using the method fragmentManager.popBackStack().

  • Joao was doing so: int id = item.getItemId();&#xA; Fragment fragment = null;&#xA; if (id == R.id.nav_camera) {&#xA; fragment = new Cronograma();&#xA; } else if (id == R.id.nav_gallery) {&#xA; fragment = new Localizacao();&#xA; } else if (id == R.id.nav_manage) {&#xA; fragment = new Sobre_aplicativo();&#xA; } else if (id == R.id.nav_share) {&#xA; finish();&#xA; } So the screen is appearing above the menu.

  • Sends as your xml of this layout.

Browser other questions tagged

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