Android棒棒糖工具栏在打开/关闭抽屉和后退按钮之间切换

前端之家收集整理的这篇文章主要介绍了Android棒棒糖工具栏在打开/关闭抽屉和后退按钮之间切换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有标准的导航抽屉,但现在我正在修改它,使用工具栏.

以前我的代码看起来像:
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,GravityCompat.START);

    mDrawerToggle = new ActionBarDrawerToggle(getActivity(),// 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);
}

并且应该显示主机功能

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

但是后箭头未显示

我试过通话

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

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

解决方法

从文档:

To allow Up navigation with the app icon in the action bar,call
setDisplayHomeAsUpEnabled():

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

This adds a left-facing caret alongside the app icon and enables it as an action
button such that when the user presses it,your activity receives a
call to onOptionsItemSelected(). The ID for the action is
android.R.id.home.

这意味着您将必须在onOptionsItemSelected上执行后台程序,并检查R.id.home.为了避免在单击汉堡包菜单调用例程,请在onOptionsItemSelected上检查是否可以进行回拨.

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();
        }
    });
}

要启用backbutton图标,只需调用getSupportActionBar().setDisplayHomeAsUpEnabled(true);禁用它只是调用mNavigationDrawerFragment.getActionBarDrawerToggle().syncState();

原文链接:https://www.f2er.com/android/313002.html

猜你在找的Android相关文章