Android棒棒糖工具栏在打开/closures抽屉和后退button之间切换

我有一个非标准的导航抽屉,但现在我试图修改它,使用工具栏

早些时候,我的代码看起来像: MainActivity.java

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.v("MAIN", "CREATE"); initViews(); setListeners(); getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() { @Override public void onBackStackChanged() { shouldDisplayHomeUp(); } }); mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer); mTitle = getTitle(); // Set up the drawer. mNavigationDrawerFragment.setUp( R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout)); } 

NavigationDrawerFragment.java

 public void setUp(int fragmentId, DrawerLayout drawerLayout) { mFragmentContainerView = getActivity().findViewById(fragmentId); mDrawerLayout = drawerLayout; mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); ((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); ((MainActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true); mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, ((MainActivity) getActivity()).getToolbar(), R.string.navigation_drawer_open, R.string.navigation_drawer_close) { @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); if (!isAdded()) { return; } getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu() } @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); if (!isAdded()) { return; } if (!mUserLearnedDrawer) { // The user manually opened the drawer; store this flag to prevent auto-showing // the navigation drawer automatically in the future. mUserLearnedDrawer = true; SharedPreferences sp = PreferenceManager .getDefaultSharedPreferences(getActivity()); sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply(); } if (mDrawerListView != null) { } getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu() } @Override public void onDrawerSlide(View drawerView, float slideOffset) { super.onDrawerSlide(drawerView, slideOffset); } }; // If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer, // per the navigation drawer design guidelines. if (!mUserLearnedDrawer && !mFromSavedInstanceState) { mDrawerLayout.openDrawer(mFragmentContainerView); } // Defer code dependent on restoration of previous instance state. mDrawerToggle.setDrawerIndicatorEnabled(true); mDrawerLayout.post(new Runnable() { @Override public void run() { mDrawerToggle.syncState(); } }); mDrawerLayout.setDrawerListener(mDrawerToggle); } 

现在我修改了我的代码: MainActivity.java

  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mToolbar = (Toolbar) findViewById(R.id.toolbar); if (mToolbar != null) { setSupportActionBar(mToolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } initViews(); setListeners(); mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer); mTitle = getTitle(); // Set up the drawer. mNavigationDrawerFragment.setUp( R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout)); shouldDisplayHomeUp(); getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() { @Override public void onBackStackChanged() { mBackCount++; shouldDisplayHomeUp(); } }); } 

NavigationDrawerFragment.java

 public void setUp(int fragmentId, DrawerLayout drawerLayout) { mFragmentContainerView = getActivity().findViewById(fragmentId); mDrawerLayout = drawerLayout; mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, ((MainActivity) getActivity()).getToolbar(), R.string.navigation_drawer_open, R.string.navigation_drawer_close) { @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); if (!isAdded()) { return; } getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu() } @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); if (!isAdded()) { return; } if (!mUserLearnedDrawer) { // The user manually opened the drawer; store this flag to prevent auto-showing // the navigation drawer automatically in the future. mUserLearnedDrawer = true; SharedPreferences sp = PreferenceManager .getDefaultSharedPreferences(getActivity()); sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply(); } if (mDrawerListView != null) { } getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu() } @Override public void onDrawerSlide(View drawerView, float slideOffset) { super.onDrawerSlide(drawerView, slideOffset); } }; // If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer, // per the navigation drawer design guidelines. if (!mUserLearnedDrawer && !mFromSavedInstanceState) { mDrawerLayout.openDrawer(mFragmentContainerView); } // Defer code dependent on restoration of previous instance state. mDrawerToggle.setDrawerIndicatorEnabled(true); mDrawerLayout.post(new Runnable() { @Override public void run() { mDrawerToggle.syncState(); } }); mDrawerLayout.setDrawerListener(mDrawerToggle); } 

并且应该显示主菜单function:

 public void shouldDisplayHomeUp (){ boolean canBack = getFragmentManager().getBackStackEntryCount() > 0; mNavigationDrawerFragment.getDrawerToggle().setDrawerIndicatorEnabled(!canBack); } 

但后退箭头没有显示

我试过电话

 getSupportActionBar().setDisplayHomeAsUpEnabled(canBack); getSupportActionBar().setHomeButtonEnabled(canBack); 

所以后箭头,但点击它没有任何效果

Solutions Collecting From Web of "Android棒棒糖工具栏在打开/closures抽屉和后退button之间切换"

从文档:

要允许使用操作栏中的应用程序图标向上导航,请调用setDisplayHomeAsUpEnabled():

 @Override public void onCreate(Bundle savedInstanceState) { ... getActionBar().setDisplayHomeAsUpEnabled(true); } 

这会在应用程序图标旁边添加一个面向左侧的插入符号,并将其作为一个操作button,以便当用户按下该button时,您的活动将接收对onOptionsItemSelected()的调用。 该动作的ID是android.R.id.home。

这意味着你将不得不在onOptionsItemSelected上实现你的后台例程,并检查R.id.home 。 当你点击汉堡包菜单时,为了避免调用例程,请在onOptionsItemSelected上检查canback

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

编辑

要实现你想要的,你将不得不实现你自己的导航程序。

  mToolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(shouldBack()) { //call onbackpressed or something if(displayBackAgain) return; //return after so you don't call syncState(); }else if (mNavigationDrawerFragment.isDrawerOpen()) mNavigationDrawerFragment.closeDrawer(); else mNavigationDrawerFragment.openDrawer(); mNavigationDrawerFragment.getActionBarDrawerToggle().syncState(); } }); } 

要启用后退button,只需调用getSupportActionBar().setDisplayHomeAsUpEnabled(true); 要禁用它只是调用mNavigationDrawerFragment.getActionBarDrawerToggle().syncState();

我find了一种方法来控制后退button和导航。 它与我合作。 一,设置:

 private void setupNav () { this.toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(this.toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); this.mActionBarDrawerToggle = new ActionBarDrawerToggle(this, this.mDrawerLayout, this.toolbar, 0, 0); this.mActionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //catch back button here. } }); this.mDrawerLayout.setDrawerListener(this.mActionBarDrawerToggle); this.mActionBarDrawerToggle.syncState(); } 

重要的是,这是我隐藏汉堡和显示后退button的方式。 你必须把这个代码放在你要显示的地方。 显示返回button时,我也locking导航。

 if (!isShowBackButton) { mActionBarDrawerToggle.setDrawerIndicatorEnabled(true); mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); } else { mActionBarDrawerToggle.setDrawerIndicatorEnabled(false); mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); //enable back button getSupportActionBar().setDisplayHomeAsUpEnabled(true); }