gpt4 book ai didi

navigation-drawer - 抽屉导航 - 慢慢动画菜单项

转载 作者:行者123 更新时间:2023-11-29 00:14:44 25 4
gpt4 key购买 nike

我用 this 实现了一个 DrawerLayout教程,一切正常。

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}

onPrepareOptionsMenu 只需一次性切换菜单可见性。但我想在抽屉滑动时缓慢地设置菜单项的 alpha 值。

最佳答案

i want it should be invisible slowly while opening the navigation drawer

实现这一点的一个非常简单的方法是利用 View.findViewsWithText .这是一个简单的例子:

public class YourActivity extends Activity {

/** Contains our {@linkplain MenuItem menu items} */
private final ArrayList<View> mMenuItems = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
// Initialize the drawer
final DrawerLayout drawer = ...;
drawer.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {

@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
animateMenuItems(slideOffset);
}

});

}

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Add some dummy items
menu.add("queue")
.setIcon(R.drawable.ic_menu_queue)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("search")
.setIcon(R.drawable.ic_menu_search)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

// Collect the menu items
final ViewGroup decor = (ViewGroup) getWindow().getDecorView();
decor.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

@Override
public void onGlobalLayout() {
// Remove any installed layout listeners
decor.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// Find each MenuItem
findMenuItems(decor, menu);
}

});
return true;
}

/** Used to traverse the decor hierarchy to locate all the menu items */
private void findMenuItems(ViewGroup decor, Menu menu) {
final int size = menu.size();
for (int i = 0; i < size; i++) {
final CharSequence title = menu.getItem(i).getTitle();
if (!TextUtils.isEmpty(title)) {
decor.findViewsWithText(mMenuItems, title, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
}
}
}

/** Used to animate each {@link MenuItem} based on the offset */
private void animateMenuItems(float slideOffset) {
for (final View view : mMenuItems) {
if (view != null) {
view.setAlpha(1f - slideOffset);
}
}
}

}

结果

http://www.gfycat.com/BriskLiquidGaur

关于navigation-drawer - 抽屉导航 - 慢慢动画菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27562507/

25 4 0