简体   繁体   English

使用导航抽屉导航时,防止多个操作栏标题发生更改

[英]Prevent multiple action bar title changes when navigating with the navigation drawer

As per the Android guidelines, I implemented an ActionBarDrawerToggle that switches to the global context onDrawerOpened and a local context onDrawerClosed . 根据Android准则,我实现了一个ActionBarDrawerToggle ,它可以切换到全局上下文onDrawerOpened和本地上下文onDrawerClosed This context switch involves changing the action bar items as well as the action bar title, and is really straightforward. 此上下文切换涉及更改操作栏项以及操作栏标题,并且非常简单。 The problem is, if I am navigating to a new screen then the action bar's title will change twice, once for the switch back to local context and again for the new screen that the user is navigating too. 问题是,如果我导航到新屏幕,则操作栏的标题将更改两次,一次是切换回本地上下文,另一次是用户也在导航的新屏幕。 This seems clunky, and I can't seem to figure out a way to implement the title change such that the user doesn't see it twice. 这似乎很笨拙,而且我似乎想不出一种实现标题更改的方法,以使用户不会两次看到它。

It seems like the best way to handle this is to have a condition you check when the drawer closes. 似乎最好的处理方法是在抽屉关闭时检查一下状况。

Here's a short example: 这是一个简短的示例:

private boolean mNoTitleChange;
private int mPosition = -1;

@Override
public void onDrawerClosed(View view) {
    if (mNoTitleChange) {
        startActivity(new Intent(CurrentActivity.this, NewActivity.class));
        mNoTitleChange = false;
        return;
    }
    getActionBar().setTitle(mTitle);
    invalidateOptionsMenu();
}

@Override
protected void onResume() {
    super.onResume();
    if (mPosition != -1) {
        setTitle(mYourTitles[mPosition]);
        mPosition = -1;
    }
}

Whenever you select an item in your DrawerLayout , adjust the boolean as needed. 每当您在DrawerLayout选择一个项目时,都可以根据需要调整boolean

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mNoTitleChange = true;
        mPosition = position;
        mDrawerLayout.closeDrawer(mDrawerList);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM