Using the Fragment classes and their layouts

This stage has three steps. First, we need to edit the menu of the Navigation Drawer to reflect the options the user has. Next, we need a View in the layout to hold whatever the active Fragment instance is, and finally, we need to add code to MainActivity.java to switch between the different Fragment instances when the user taps on the menu.

Editing the Navigation Drawer menu

Open the activity_main_drawer.xml file in the res/menu folder of the project explorer. Edit the code within the group tags that we saw earlier to reflect our menu options of Insert, Delete, Search, and Results:

<group android:checkableBehavior="single">
   <item
         android:id="@+id/nav_insert"
         android:icon="@drawable/ic_menu_camera"
         android:title="Insert" />
   <item
         android:id="@+id/nav_delete"
         android:icon="@drawable/ic_menu_gallery"
         android:title="Delete" />
   <item
         android:id="@+id/nav_search"
         android:icon="@drawable/ic_menu_slideshow"
         android:title="Search" />
   <item
         android:id="@+id/nav_results"
         android:icon="@drawable/ic_menu_manage"
         android:title="Results" />
</group>

Tip

Now would be a good time to add new icons to the drawable folder and edit the preceding code to refer to them if you wanted to use your own icons.

Adding a holder to the main layout

Open the content_main.xml file in the layout folder and add this highlighted XML code just before the closing tag of the ConstraintLayout:

    <FrameLayout
        android:id="@+id/fragmentHolder"
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp">
    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Now we have a FrameapplyLayout with an id attribute of fragmentHolder that we can get a reference to and load all our Fragment instance layouts into.

Coding the MainActivity.java

Open the MainActivity file and edit the onNavigationItemSelected method to handle all the different menu options the user can choose from:

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
   // Handle navigation view item clicks here.

   // Create a transaction
   FragmentTransaction transaction 
          = getSupportFragmentManager().beginTransaction();

   int id = item.getItemId();

   if (id == R.id.nav_insert) {
         // Create a new fragment of the appropriate type
         InsertFragment fragment = new InsertFragment();
         // What to do and where to do it
         transaction.replace(R.id.fragmentHolder, fragment);

   } else if (id == R.id.nav_search) {
         SearchFragment fragment = new SearchFragment();
         transaction.replace(R.id.fragmentHolder, fragment);

   } else if (id == R.id.nav_delete) {
         DeleteFragment fragment = new DeleteFragment();
         transaction.replace(R.id.fragmentHolder, fragment);

   }  else if (id == R.id.nav_results) {
         ResultsFragment fragment = new ResultsFragment();
         transaction.replace(R.id.fragmentHolder, fragment);
   }

   // Ask Android to remember which
   // menu options the user has chosen
   transaction.addToBackStack(null);

   // Implement the change
   transaction.commit();

   DrawerLayout drawer = (DrawerLayout) 
         findViewById(R.id.drawer_layout);
         
   drawer.closeDrawer(GravityCompat.START);
   
   return true;
}

Let's go through the code we just added. Most of the code should look familiar. For each of our menu options, we create a new Fragment of the appropriate type and insert it into our RelativeLayout with an id of fragmentHolder.

The transaction.addToBackStack method means that the chosen Fragment will be remembered in order with any others. The result of this is that if the user chooses the insert fragment, then the results fragment taps the back button, and then the app will return the user to the insert fragment.

You can now run the app and use the Navigation Drawer menu to flip between all our different Fragment instances. They will look just as they did in the images at the start of this chapter, but they don't have any functionality yet.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.171.86