隐藏动作条的MenuItems当导航抽屉滑轨,只要金额 [英] Hide ActionBar MenuItems when Navigation Drawer slides for any amount
问题描述
我想实现一个抽屉式导航,它隐藏在每当打开抽屉的动作条中的菜单项。
I'm trying to implement a Navigation Drawer that hides the menu items in the ActionBar whenever the drawer is opened.
我下面谷歌的文档,但他们的code没有产生预期的行为。
I am following google's documentation, however their code does not produce the expected behavior.
<一个href="http://developer.android.com/training/implementing-navigation/nav-drawer.html">http://developer.android.com/training/implementing-navigation/nav-drawer.html
使用这个code,当抽屉变的完全打开的菜单项是隐藏的,当抽屉变成完全封闭所示。
Using this code, the menu items are hidden when the drawer becomes completely opened , and shown when the drawer becomes completely closed.
不过,Gmail应用程序的行为有所不同。菜单项目一旦抽屉的打开任意量的隐藏。这就是我想要的行为。有谁知道如何实现这一目标?
However, the Gmail app behaves differently. The menu items are hidden as soon as the drawer opens by any amount. This is the behavior I want. Does anyone know how to achieve this?
谢谢!
推荐答案
你有没有尝试过这样的:
Have you tried this:
- 使用
invalidateOptionsMenu()
当你切换导航抽屉,通过测量滑动偏移。 -
迭代的
prepareOptionsMenu(功能菜单)上的每个菜单项
键,将其隐藏。
- Use
invalidateOptionsMenu()
whenever you toggle the nav drawer, by measuring the sliding offset. Iterate over each menu item in
onPrepareOptionsMenu(Menu menu)
and hide it.
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = shouldGoInvisible;
hideMenuItems(menu, !drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
private void hideMenuItems(Menu menu, boolean visible)
{
for(int i = 0; i < menu.size(); i++){
menu.getItem(i).setVisible(visible);
}
}
检测多少资产净值抽屉滑动了:
Detecting how much the nav drawer has slided:
mDrawerLayout.setDrawerListener(new DrawerListener(){
float mPreviousOffset = 0f;
@Override
public void onDrawerClosed(View arg0) {
super.onDrawerClosed(arg0);
shouldGoInvisible = false;
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
@Override
public void onDrawerOpened(View arg0) {
super.onDrawerOpened(arg0);
shouldGoInvisible = true;
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
@Override
public void onDrawerSlide(View arg0, float slideOffset) {
super.onDrawerSlide(arg0, slideOffset);
if(slideOffset > mPreviousOffset && !shouldGoInvisible){
shouldGoInvisible = true;
invalidateOptionsMenu();
}else if(mPreviousOffset > slideOffset && slideOffset < 0.5f && shouldGoInvisible){
shouldGoInvisible = false;
invalidateOptionsMenu();
}
mPreviousOffset = slideOffset;
}
@Override
public void onDrawerStateChanged(int arg0) {
// or use states of the drawer to hide/show the items
}});
注: shouldGoInvisible
是类的字段。
这篇关于隐藏动作条的MenuItems当导航抽屉滑轨,只要金额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!