如何在Android中的NavigationUI中添加动画过渡?

如何在Android中的NavigationUI中添加动画过渡?

问题描述:

我正在使用NavigationUI将目标绑定到菜单项,但是如何覆盖默认的动画过渡?

i'm using NavigationUI to tie destinations to menu items, but how to override the default animation transition?

基于文档 https://developer.android.com/topic/libraries/architecture/navigation/navigation-ui#Tie-navdrawer ,我找不到任何可以添加动画过渡的方法.

Based on the doc https://developer.android.com/topic/libraries/architecture/navigation/navigation-ui#Tie-navdrawer, i cant find any method that can add the animation transition.

NavigationUI不提供该API.但是,绝对不需要使用 NavigationUI -它只是可选的辅助方法.

NavigationUI does not offer that API. However, there's absolutely no requirement to use NavigationUI - it is only optional helper methods.

因此,您可以复制/构建

Therefore you can copy / build a simplified version of what it actually does:

NavOptions navOptions = new NavOptions.Builder()
    .setLaunchSingleTop(true)  // Used to prevent multiple copies of the same destination
    .setEnterAnim(R.anim.your_enter_anim)
    .setExitAnim(R.anim.your_exit_anim)
    .setPopEnterAnim(R.anim.your_pop_enter_anim)
    .setPopExitAnim(R.anim.your_pop_exit_anim);
    .build();

// Assuming you have a MenuItem named item
navController.navigate(item.getItemId(), null, options);