Skip to content Skip to sidebar Skip to footer

Drill Down Navigation With Fragments From Navigationdrawer

So I've been able to implement the built in NavigationDrawer in my Android app without any issues, and all my main Fragments are setup and work when selected. Where I'm stuck is th

Solution 1:

Yes you need to add/replace fragments to the container based on list item click

You can use interface as a callback to the activity.

Fragment1-->HostingActivity-->Fragment2. Add make sure you add fragments to backstack.

http://developer.android.com/training/basics/fragments/communicating.html

You can use

getActivity().getActionBar().setHomeButtonEnabled(true);
 getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);

More info @

http://developer.android.com/training/implementing-navigation/ancestral.html

Example :

publicinterfaceListItemCallback
{
      publicvoidcallBackList()
}

Then in fragment

ListItemCallback mCallback;
@OverridepublicvoidonAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented// the callback interface. If not, it throws an exceptiontry {
        mCallback = (ListItemCallback) activity;
    } catch (ClassCastException e) {
        thrownewClassCastException(activity.toString()
                + " must implement ListItemCallback");
    }
}

Then

 clientListView.setOnItemClickListener(newAdapterView.OnItemClickListener() {
        @OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
            Stringname= dataList.get(position).clientName;
            Log.d("message", "the client clicked on is: " + name);
            mCallback.callBackList();

        }
    });

Then in Activity

publicclassMainActivityextendsActivityimplementsListItemCallback
 {
  ...// rest of the code@OverridepublicvoidcallBackList(String name)
  {
        Fragmentfragment=newFragmentClientDetail();
        FragmentManagermanager= getFragmentManager();
        FragmentTransactiontransaction= manager.beginTransaction();
        transaction.replace(R.id.container, fragment);
        transaction.addToBackStack("clientdetail");
        transaction.commit(); 
  } 

 }

Solution 2:

Okay, so after a lot of reading and digging through the android docs I finally got it setup so that the subviews show the up caret and works as the back button, but when the main view is displayed then the navigation drawer is shown again. Here's what I ended up doing in case anyone else is trying to do the same (or if anyone finds a problem with how I did this and have a better solution).

//First in the NavigationDrawerFragment class that is created with the//Drawer template I added two methods, that will adjust the drawer's view//change action bar to show up caretpublicstaticvoidshowSubActionBar() {
        mDrawerToggle.setDrawerIndicatorEnabled(false);
    }
    //change action bar to show navigation drawer iconpublicstaticvoidshowNavActionBar() {
        mDrawerToggle.setDrawerIndicatorEnabled(true);
    }

//Then in my MainActivity class I add a small method that uses a counter// to determine if a user is in a sub level (since some //fragments may have up to 4 sub levels) or back on the main view //and updates the ActionBarpublicvoidsubLevelCounter(int counter) {

       levelCounter = levelCounter + counter;
        if (levelCounter > 0) {
            NavigationDrawerFragment.showSubActionBar();
        }
        else {
            NavigationDrawerFragment.showNavActionBar();

        }
        invalidateOptionsMenu();

    }

//So now when the back button is visible and pressed I updated the counter//and call the onBackPressed methodif (item.getItemId() == android.R.id.home) {
            ((MainActivity) getActivity()).subLevelCounter(-1);
            getActivity().onBackPressed();
            returntrue;
        }

//And when I'm drilling down I just add this line before calling the //backlist method to perform the navigation
                ((MainActivity) thisActivity).subLevelCounter(1);

Post a Comment for "Drill Down Navigation With Fragments From Navigationdrawer"