2014-12-13 65 views
0

我實施了BaseActivity,它擴展了ActionBarActivity並實現了NavigationDrawer。我所有的Activities都繼承了這個BaseActivity。 我現在想知道如果我可以實現NavigationDrawer,並仍然使用工具欄正確的向上導航,或者是當我實施NavigationDrawer時設備後退按鈕應該作爲向上導航按鈕嗎?NavigationDrawer and Up導航

編輯:

定製NavigationDrawerToolbar

public abstract class NavigationActivity extends ActionBarActivity { 
    private Toolbar toolbar = null; 
    private DrawerLayout drawerLayout = null; 
    private ActionBarDrawerToggle drawerToggle = null;; 
    private ListView listView = null; 
    private CharSequence drawerTitle = null; 
    private CharSequence title = null; 
    private NavigationDrawerConfiguration navigationConfig = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     this.navigationConfig = this.getNavigationDrawerConfiguration(); 
     this.setContentView(this.navigationConfig.getMainLayout()); 

     this.toolbar = (Toolbar) this.findViewById(R.id.app_bar); 
     this.drawerLayout = (DrawerLayout) this.findViewById(navigationConfig.getDrawerLayoutId()); 
     this.listView = (ListView) this.findViewById(navigationConfig.getLeftDrawerId()); 
     this.listView.setAdapter(navigationConfig.getBaseAdapter()); 
     this.listView.setOnItemClickListener(new DrawerItemClickListener()); 
     this.drawerTitle = this.getTitle(); 
     this.title = this.getTitle(); 

     this.setSupportActionBar(this.toolbar); 
     this.getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     this.getSupportActionBar().setHomeButtonEnabled(true); 

     this.drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, this.toolbar, this.navigationConfig.getDrawerOpenDesc(), this.navigationConfig.getDrawerCloseDesc()) { 
      @Override 
      public void onDrawerClosed(View view) { 
       getSupportActionBar().setTitle(title); 
       invalidateOptionsMenu(); 
      } 
      @Override 
      public void onDrawerOpened(View drawerView) { 
       getSupportActionBar().setTitle(drawerTitle); 
       invalidateOptionsMenu(); 
      } 
     }; 

     this.drawerLayout.setDrawerListener(this.drawerToggle); 
    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 

     this.drawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 

     this.drawerToggle.onConfigurationChanged(newConfig); 
    } 

    @Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
     if (this.navigationConfig.getActionMenuItemsToHideWhenDrawerOpen() != null) { 
      boolean drawerOpen = this.drawerLayout.isDrawerOpen(listView); 

      for (int item : this.navigationConfig.getActionMenuItemsToHideWhenDrawerOpen()) { 
       menu.findItem(item).setVisible(!drawerOpen); 
      } 
     } 

     return super.onPrepareOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (this.drawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } else { 
      return false; 
     } 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_MENU) { 
      if (this.drawerLayout.isDrawerOpen(this.listView)) { 
       this.drawerLayout.closeDrawer(this.listView); 
      } else { 
       this.drawerLayout.openDrawer(this.listView); 
      } 

      return true; 
     } 

     return super.onKeyDown(keyCode, event); 
    } 

    protected NavigationDrawerConfiguration getNavigationDrawerConfiguration() { 

     NavigationDrawerItem[] menu = new NavigationDrawerItem[] { 
//    NavigationMenuSection.create(100, "Demos"), 
       NavigationMenuItem.create(100, "Item 1", "ic_measure", false, this), 
       NavigationMenuItem.create(200, "Item 2", "ic_list", false, this), 
//    NavigationMenuSection.create(200, "General"), 
       NavigationMenuItem.create(300, "Item 3", "ic_diagrams", false, this), 
       NavigationMenuItem.create(400, "Item 4", "ic_calculator", false, this), 
       NavigationMenuItem.create(500, "Item 5", "ic_scanner", false, this), 
       NavigationMenuItem.create(600, "Item 6", "ic_profile", false, this), 
       NavigationMenuItem.create(700, "Item 7", "ic_follower", false, this), 
       NavigationMenuItem.create(800, "Item 8", "ic_settings", false, this) 
     }; 

     NavigationDrawerConfiguration navigationDrawerConfiguration = new NavigationDrawerConfiguration(); 
     navigationDrawerConfiguration.setMainLayout(R.layout.activity_menu_slide); 
     navigationDrawerConfiguration.setDrawerLayoutId(R.id.menu_slide_layout); 
     navigationDrawerConfiguration.setLeftDrawerId(R.id.menu_slide_list); 
     navigationDrawerConfiguration.setNavigationItems(menu); 
     navigationDrawerConfiguration.setDrawerOpenDesc(R.string.open_navigation_drawer); 
     navigationDrawerConfiguration.setDrawerCloseDesc(R.string.close_navigation_drawer); 
     navigationDrawerConfiguration.setBaseAdapter(new NavigationDrawerAdapter(this, R.layout.activity_menu_slide_item_row, menu)); 

     return navigationDrawerConfiguration; 
    } 

    public void selectItem(int position) { 
     NavigationDrawerItem selectedItem = this.navigationConfig.getNavigationItems()[position]; 

     this.onNavigationItemSelected(selectedItem.getId()); 
     this.listView.setItemChecked(position, true); 

     if (selectedItem.updateActionBarTitle()) { 
      this.setTitle(selectedItem.getLabel()); 
     } 

     if (this.drawerLayout.isDrawerOpen(this.listView)) { 
      this.drawerLayout.closeDrawer(this.listView); 
     } 
    } 

    protected void onNavigationItemSelected(int id) { 
     Intent intent = null; 

     switch ((int) id) { 
     case 100: 
      intent = new Intent(this, MeasureDataActivity.class); 
      break; 
     case 200: 
      intent = new Intent(this, MeasureDataListActivity.class); 
      break; 
     case 300: 
      intent = new Intent(this, DiagramsActivity.class); 
      break; 
     case 400: 
      intent = new Intent(this, CalcActivity.class); 
      break; 
     case 500: 
      intent = new Intent(this, RecipeActivity.class); 
      break; 
     case 600: 
      intent = new Intent(this, ProfileActivity.class); 
      break; 
     case 700: 
      intent = new Intent(this, FollowerActivity.class); 
      break; 
     case 800: 
      intent = new Intent(this, SettingsActivity.class); 
      break; 
     } 

     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     this.startActivity(intent); 
    } 

    protected int getDrawerIcon() { 
     return R.drawable.ic_navigation_drawer; 
    } 

    protected DrawerLayout getDrawerLayout() { 
     return this.drawerLayout; 
    } 

    protected ActionBarDrawerToggle getDrawerToggle() { 
     return this.drawerToggle; 
    } 

    @Override 
    public void setTitle(CharSequence title) { 
     this.title = title; 
     this.getSupportActionBar().setTitle(title); 
    } 

    private class DrawerItemClickListener implements ListView.OnItemClickListener { 
     /** 
     * 
     */ 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      selectItem(position); 
     } 
    } 
} 

回答

1

我認爲在OnCreate超()調用之後,你可以這樣做:

mDrawerToggle.setDrawerIndicatorEnabled(false); 

如果這本身並不能工作(或只是不工作)試試這個:

getActionBar().setDisplayHomeAsUpEnabled(true); 

請點擊此鏈接:Missing Up navigation icon after switching from ICS ActionBar to Lollipop Toolbar

原始海報發現這個鏈接

+0

沒有這不起作用。如果我禁用DrawerIndicator我不能按工具欄中的任何東西。如果我添加DisplayHomeAsUpEnabled(true),那麼在工具欄中也沒有任何東西需要按下。它們使用兩者的組合相同。 – Mulgard 2014-12-13 14:45:14

+0

@Mulgard我剛剛創建了自己的項目,如果我做了'mDrawerToggle.setDrawerIndicatorEnabled(false);'它顯示一個後退按鈕。但是你提到你有一個工具欄,你有一些示例代碼嗎? – 2014-12-13 15:01:54

+0

是的,我添加了NavigationDrawer的示例代碼。我使用工具欄,因爲所有的ActionBar的東西已被棄用,因爲5.0 – Mulgard 2014-12-13 15:24:16