Missing Up navigation icon after switching from ICS ActionBar to Lollipop Toolbar

前端 未结 3 756
南旧
南旧 2020-12-29 11:13

I have an activity with many fragments that uses action bar and navigation drawer. It has \"home as up\" enabled. I have implemented proper logic that only top level fragmen

相关标签:
3条回答
  • 2020-12-29 11:30

    If you use AppCompatActivity, you can get the right drawer icon and back icon by

            if(homeUp)
        {
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            mDrawerToggle.setDrawerIndicatorEnabled(false);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
    
        }
        else
        {
            mDrawerToggle.setDrawerIndicatorEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mDrawerToggle.syncState();
        }
    

    Without need for getV7DrawerToggleDelegate :D

    0 讨论(0)
  • 2020-12-29 11:31

    Answer/comments of Nikola Despotoski and Andrey Novikov are perfectly correct but I want to mention that after toolbar was replaced with following code:

    drawerToggle.setDrawerIndicatorEnabled(false);
    drawerToggle.setHomeAsUpIndicator(getV7DrawerToggleDelegate().getThemeUpIndicator());
    setSupportActionBar(toolbar);
    

    your activity will receive every onOptionsItemsSelected events even if you enable your drawer toogle again drawerToggle.setDrawerIndicatorEnabled(true); So you need to handle this, I've ended with

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                if (drawerToggle.isDrawerIndicatorEnabled()) {
                    return drawerToggle.onOptionsItemSelected(item);
                } else {
                    onBackPressed();
                    return true;
                }
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    0 讨论(0)
  • 2020-12-29 11:42

    Have you tried to get themed up indicator using getV7DrawerToggleDelegate().getThemeUpIndicator () and set it after you disable the indicator?

    Because when the indicator is disabled ActionBarDrawerToggle tries to set the previous indicator.

    From ActionBarDrawerToggle source:

    public void setDrawerIndicatorEnabled(boolean enable) {
        if (enable != mDrawerIndicatorEnabled) {
            if (enable) {
                setActionBarUpIndicator((Drawable) mSlider,
                        mDrawerLayout.isDrawerOpen(GravityCompat.START) ?
                                mCloseDrawerContentDescRes : mOpenDrawerContentDescRes);
            } else {
                setActionBarUpIndicator(mHomeAsUpIndicator, 0);
            }
            mDrawerIndicatorEnabled = enable;
        }
    }
    

    Edit:

    As of deprecation of ActionBarActivity, you should use getDrawerToggleDelegate().getThemeUpIndicator ()

    0 讨论(0)
提交回复
热议问题