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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章