问题
I'm using the new DrawerLayout
to have side navigation. I'm using the drawer icon (the 'hamburger') like this:
@Override
protected void onStart() {
super.onStart();
mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_main_drawerlayout);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_navigation_drawer,
R.string.app_name,
R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
However, when I add a Fragment
to the backstack, I want to display the back arrow again, so the user can navigate back to "home", and only then open the app drawer.
How can I reset the drawer icon to the back icon?
The arrow I want:
回答1:
To disable and hide the DrawerToggle "Hamburger", just call
mDrawerToggle.setDrawerIndicatorEnabled(false);
回答2:
I created an interface for the hosting activity to update the view state of the hamburger menu. For top level fragments I set the toggle to true
and for fragments for which I want to display the up < arrow I set the toggle to false
.
public class SomeFragment extends Fragment {
public interface OnFragmentInteractionListener {
public void showDrawerToggle(boolean showDrawerToggle);
}
private OnFragmentInteractionListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
this.mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener");
}
}
@Override
public void onResume() {
super.onResume();
mListener.showDrawerToggle(false);
}
}
Then in my Activity ...
public class MainActivity extends Activity implements SomeFragment.OnFragmentInteractionListener {
private ActionBarDrawerToggle mDrawerToggle;
public void showDrawerToggle(boolean showDrawerIndicator) {
mDrawerToggle.setDrawerIndicatorEnabled(showDrawerIndicator);
}
}
来源:https://stackoverflow.com/questions/17750035/change-drawer-icon-back-to-back-arrow