我实施了BaseActivity
,扩展了ActionBarActivity
并实现了NavigationDrawer
。我的所有Activities
都继承自此BaseActivity
。
我现在想知道我是否可以实现NavigationDrawer
并且仍然使用工具栏进行适当的向上导航,或者当我实现NavigationDrawer
时,设备后退按钮是否应该用作向上导航按钮?
编辑:
自定义NavigationDrawer
Toolbar
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);
}
}
}
答案 0 :(得分:1)
我认为在super()调用之后的onCreate中你可以这样做:
mDrawerToggle.setDrawerIndicatorEnabled(false);
如果仅此一项不起作用(或只是不起作用),请尝试以下方法:
getActionBar().setDisplayHomeAsUpEnabled(true);
请点击此链接:Missing Up navigation icon after switching from ICS ActionBar to Lollipop Toolbar
原始海报发现此链接