NavigationView和ActionBarDrawerToggle [英] NavigationView and ActionBarDrawerToggle
问题描述
随着新的 NavigationView
是它仍然推荐使用 ActionBarDrawerToggle
或这是不是材料设计?例如previously我们应该隐藏动作栏项目的抽屉被打开的时候,但现在的指导方针说,他们应该留下来。
随着新的
NavigationView
是它仍然推荐使用ActionBarDrawerToggle
块引用>没有,它不是必需的。
如果你看一下官方演示code为新的设计库,
ActionBarDrawerToggle
不再使用,因为新的NavigationView
和AppCompatActivity
并不真正需要的吧。随着新的V22支持库,你可以去掉所有的
ActionBarDrawerToggle
code的,只是使用以下方法来处理NavigationDrawer
和动作条
/工具栏
汉堡图标:@覆盖
保护无效的onCreate(捆绑savedInstanceState){
...
最后的动作条动作条= getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
actionBar.setDisplayHomeAsUpEnabled(真);
...
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
返回true;
....
}
返回super.onOptionsItemSelected(项目);
}您需要提供自己的汉堡包绘(
R.drawable.ic_menu
在我的例子)。除此之外,上述code是所有的需要办理抽屉打开。在android.R.id.home
情况onOptionsItemSelected()
重新presents你的汉堡包纸盒键。它指向一个内置的资源ID(不可以你的东西添加到您的菜单XML),它的自动处理。除此之外,您可以通过简单地添加
closeDrawers()
来点击监听器,这样来实现抽屉的结束:navigationView.setNavigationItemSelectedListener(
新NavigationView.OnNavigationItemSelectedListener(){
@覆盖
公共布尔onNavigationItemSelected(菜单项菜单项){
//处理菜单项,点击这里。
drawerLayout.closeDrawers();
返回true;
}
});
closeDrawers()
是DrawerLayout的方法,并完成所有的操作。而已。这是所有的code,你现在真正需要妥善处理导航抽屉。没有更多的凌乱code的翻转汉堡包和这样!当然,如果你真的想,你仍然可以使用
NavigationView
与ActionBarDrawerToggle
的老路上。但你肯定没有。如果您想抽屉回调
虽然
ActionBarDrawerToggle
不需要打开/关闭抽屉,它可能仍然是处理其他回调有用的(特别是如果你使用的动作条的话)。否则,您可以通过实现自己的DrawerLayout.DrawerListener
通过使用或DrawerLayout.SimpleDrawerListener()
,以处理其他打开/关闭相关的事件。With the new
NavigationView
is it still recommended to useActionBarDrawerToggle
or is this not "Material Design"? For instance previously we were supposed to hide action bar items when the drawer was opened but now the guidelines say that they should stay.解决方案With the new
NavigationView
is it still recommended to useActionBarDrawerToggle
No, it's not required.
If you look at the "official" demo code for the new Design Library,
ActionBarDrawerToggle
is no longer used, since the newNavigationView
andAppCompatActivity
don't really need it.With the new v22 support library, you can strip out all of your
ActionBarDrawerToggle
code and just use the following to handle the interaction betweenNavigationDrawer
and theActionBar
/ToolBar
hamburger icon:@Override protected void onCreate(Bundle savedInstanceState) { ... final ActionBar actionBar = getSupportActionBar(); actionBar.setHomeAsUpIndicator(R.drawable.ic_menu); actionBar.setDisplayHomeAsUpEnabled(true); ... } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: mDrawerLayout.openDrawer(GravityCompat.START); return true; .... } return super.onOptionsItemSelected(item); }
You will need to provide your own "hamburger" drawable (
R.drawable.ic_menu
in my example). Besides that, the above code is all that's needed to handle opening of the drawer. Theandroid.R.id.home
case inonOptionsItemSelected()
represents your hamburger drawer button. It points to a built-in resource id (not something you add to you menu xml), and it's handled automatically.Besides that, you have to implement closing of the drawer by simply adding
closeDrawers()
to your click listener, like this:navigationView.setNavigationItemSelectedListener( new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem menuItem) { // Handle menu item clicks here. drawerLayout.closeDrawers(); return true; } });
closeDrawers()
is a method of DrawerLayout, and takes care of everything. That's it. That's all the code you really need to properly handle navigation drawers now. No more messy code for flipping hamburgers and such!Of course, if you really want to, you can still use
NavigationView
withActionBarDrawerToggle
the old way. But you certainly don't have to.If you want drawer callbacks
Even though
ActionBarDrawerToggle
is not required to open/close the drawer, it may still be useful for handling additional callbacks (especially if you're using ActionBar already). Otherwise, you can implement your own by usingDrawerLayout.DrawerListener
or by usingDrawerLayout.SimpleDrawerListener()
, to handle other open/close related events.这篇关于NavigationView和ActionBarDrawerToggle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!