Switching between Android Navigation Drawer image and Up caret when using fragments

问题:

When using the Navigation Drawer the Android devs are recommending that in the ActionBar "only those screens that are represented in the Navigation Drawer should actually have the Navigation Drawer image" and that "all other screens have the traditional up carat." 当使用导航抽屉时,Android开发人员建议在ActionBar中“只有那些在导航抽屉中表示的屏幕应该实际上具有导航抽屉图像”,并且“所有其他屏幕都具有传统的向上克拉”。

See here for details: http://youtu.be/F5COhlbpIbY 详情请见:http: //youtu.be/F5COhlbpIbY

I'm using one activity to control multiple levels of fragments and can get the Navigation Drawer image to display and function at all levels. 我正在使用一个活动来控制多个级别的片段,并且可以使导航抽屉图像在所有级别上显示和运行。

When creating lower level fragments I can call the ActionBarDrawerToggle setDrawerIndicatorEnabled(false) to hide the Navigation Drawer image and have the Up caret displayed 创建较低级别的片段时,我可以调用ActionBarDrawerToggle setDrawerIndicatorEnabled(false)来隐藏导航抽屉图像并显示Up插入符号

LowerLevelFragment lowFrag = new LowerLevelFragment();

//disable the toggle menu and show up carat
theDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportFragmentManager().beginTransaction().replace(R.id.frag_layout, 
lowFrag, "lowerFrag").addToBackStack(null).commit();

The problem I'm having is when I navigate back to the top level fragments the Up carat still shows instead of the original Navigation Drawer image. 我遇到的问题是当我导航回到最高级别的片段时,向上克拉仍然显示而不是原始的导航抽屉图像。 Any suggestions on how to "refresh" the ActionBar on the top level fragments to re-display the Navigation Drawer image? 有关如何“刷新”顶级片段上的ActionBar以重新显示导航抽屉图像的任何建议?


Solution

Tom's suggestion worked for me. 汤姆的建议对我有用。 Here's what I did: 这是我做的:

MainActivity 主要活动

This activity controls all fragments in the app. 此活动控制应用程序中的所有片段。

When preparing new fragments to replace others, I set the DrawerToggle setDrawerIndicatorEnabled(false) like this: 在准备新片段以替换其他片段时,我将DrawerToggle setDrawerIndicatorEnabled(false)设置为:

LowerLevelFragment lowFrag = new LowerLevelFragment();

//disable the toggle menu and show up carat
theDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportFragmentManager().beginTransaction().replace(R.id.frag_layout,   
lowFrag).addToBackStack(null).commit();

Next, in an override of onBackPressed , I reverted the above by setting the DrawerToggle to setDrawerIndicatorEnabled(true) like this: 接下来,在onBackPressed的覆盖中,我通过将DrawerToggle设置为setDrawerIndicatorEnabled(true)来恢复上述内容,如下所示:

@Override
public void onBackPressed() {
    super.onBackPressed();
    // turn on the Navigation Drawer image; 
    // this is called in the LowerLevelFragments
    setDrawerIndicatorEnabled(true)
}

In the LowerLevelFragments 在LowerLevelFragments中

In the fragments I modified onCreate and onOptionsItemSelected like this: 在我修改onCreateonOptionsItemSelected的片段中,如下所示:

In onCreate added setHasOptionsMenu(true) to enable configuring the options menu. onCreate添加了setHasOptionsMenu(true)以启用配置选项菜单。 Also set setDisplayHomeAsUpEnabled(true) to enable the < in the actionbar: 同时设置setDisplayHomeAsUpEnabled(true)以在操作栏中启用<

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // needed to indicate that the fragment would 
    // like to add items to the Options Menu        
    setHasOptionsMenu(true);    
    // update the actionbar to show the up carat/affordance 
    getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
}

Then in onOptionsItemSelected whenever the < is pressed it calls the onBackPressed() from the activity to move up one level in the hierarchy and display the Navigation Drawer Image: 然后在onOptionsItemSelected每当按下<时,它从活动中调用onBackPressed()以在层次结构中向上移动一级并显示导航抽屉图像:

@Override
public boolean onOptionsItemSelected(MenuItem item) {   
    // Get item selected and deal with it
    switch (item.getItemId()) {
        case android.R.id.home:
            //called when the up affordance/carat in actionbar is pressed
            getActivity().onBackPressed();
            return true;
        … 
    }

解决方案:

参考一: https://en.stackoom.com/question/1APbA
参考二: https://stackoom.com/question/1APbA
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章