如何覆盖抽屉导航按钮行为并更改图标?

我的应用程序使用抽屉导航来显示具有主要主题的不同片段。抽屉导航

在“主页”片段中,我显示不同的类别(那些彩色框),当我选择一个类别时,它会显示带有该类别的日常和一般详细信息的新片段。(我们称之为“详细信息片段” 

我想要但无法弄清楚如何完成的是在该详细信息片段中,我希望有返回箭头将我带回主页片段,而不是打开抽屉导航的汉堡菜单图标。 细节片段


翻翻过去那场雪
浏览 63回答 3
3回答

郎朗坤

如果我假设您在布局中使用 android.support.v4.widget.DrawerLayout,那么这种方法可能适合您;我只在 API 21 上进行了测试,但考虑到它主要使用支持库,它应该可以在较低或较高的目标上工作(著名的最后一句话)。导入 android.support.v7.app.ActionBarDrawerToggle 导入 android.support.v4.widget.DrawerLayoutActionBarDrawerToggle mDrawerToggle;DrawerLayout drawerLayout;private boolean mToolBarNavigationListenerIsRegistered = false;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setSupportActionBar(mToolbar);    getSupportActionBar().setDisplayShowTitleEnabled(false);    // Get DrawerLayout ref from layout    drawerLayout = (DrawerLayout)findViewById(R.id.drawer);    // Initialize ActionBarDrawerToggle, which will control toggle of hamburger.    // You set the values of R.string.open and R.string.close accordingly.    // Also, you can implement drawer toggle listener if you want.    mDrawerToggle = new ActionBarDrawerToggle (this, drawerLayout, mToolbar, R.string.open, R.string.close);    // Setting the actionbarToggle to drawer layout    drawerLayout.setDrawerListener(mDrawerToggle);    // Calling sync state is necessary to show your hamburger icon...    // or so I hear. Doesn't hurt including it even if you find it works    // without it on your test device(s)    mDrawerToggle.syncState();}/** * To be semantically or contextually correct, maybe change the name * and signature of this function to something like: * * private void showBackButton(boolean show) * Just a suggestion. */ private void enableViews(boolean enable) {    // To keep states of ActionBar and ActionBarDrawerToggle synchronized,    // when you enable on one, you disable on the other.    // And as you may notice, the order for this operation is disable first, then enable - VERY VERY IMPORTANT.    if(enable) {        //You may not want to open the drawer on swipe from the left in this case          drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);        // Remove hamburger        mDrawerToggle.setDrawerIndicatorEnabled(false);        // Show back button        getSupportActionBar().setDisplayHomeAsUpEnabled(true);        // when DrawerToggle is disabled i.e. setDrawerIndicatorEnabled(false), navigation icon        // clicks are disabled i.e. the UP button will not work.        // We need to add a listener, as in below, so DrawerToggle will forward        // click events to this listener.        if(!mToolBarNavigationListenerIsRegistered) {            mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    // Doesn't have to be onBackPressed                    onBackPressed();                }            });            mToolBarNavigationListenerIsRegistered = true;        }    } else {        //You must regain the power of swipe for the drawer.         drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);        // Remove back button        getSupportActionBar().setDisplayHomeAsUpEnabled(false);        // Show hamburger         mDrawerToggle.setDrawerIndicatorEnabled(true);        // Remove the/any drawer toggle listener        mDrawerToggle.setToolbarNavigationClickListener(null);        mToolBarNavigationListenerIsRegistered = false;    }    // So, one may think "Hmm why not simplify to:    // .....    // getSupportActionBar().setDisplayHomeAsUpEnabled(enable);    // mDrawer.setDrawerIndicatorEnabled(!enable);    // ......    // To re-iterate, the order in which you enable and disable views IS important #dontSimplify.}该解决方案使用 ActionBarDrawerToggle.setDrawerIndicatorEnabled 来切换汉堡包图标的可见性,使用 ActionBar.setDisplayHomeAsUpEnabled 来切换向上按钮的可见性,本质上是利用它们各自的可绘制资源。

蛊毒传说

如果您认为这是不好的解决方案,请告诉我。这就是我能够使用非常简单的解决方案(Kotlin)使其工作的方法:将其放入您的活动 onCreate 中supportFragmentManager.addOnBackStackChangedListener {       actionBarDrawerToggle?.isDrawerIndicatorEnabled = supportFragmentManager.backStackEntryCount <= 0}并重写 onSupportNavigateUp():override fun onSupportNavigateUp(): Boolean {    supportFragmentManager.popBackStack()    return super.onSupportNavigateUp()}

DIEA

您可以通过动态地将工具栏添加到主活动来完成此操作,而不是使用内置的导航抽屉,从头开始制作。添加片段时在片段上添加工具栏。工具栏 myChildToolbar = (工具栏) findViewById(R.id.my_child_toolbar); setSupportActionBar(myChildToolbar);// Get a support ActionBar corresponding to this toolbarActionBar ab = getSupportActionBar();// Enable the Up buttonab.setDisplayHomeAsUpEnabled(true);然后,您将能够重写按钮导航向上方法上的方法,以在工具栏上启用您想要的后退按钮。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java