2014-12-13 40 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