【问题标题】:Animation for BottomNavigation Fragments with Architecture Navigation Components带有架构导航组件的底部导航片段的动画
【发布时间】:2020-05-01 11:18:10
【问题描述】:

我已经成功地将底部导航与最新的android架构导航组件集成在一起。以下是我的完整代码。

  1. 导航
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_home">

    <fragment
        android:id="@+id/navigation_home"
        android:name="in.zedone.bottomsample.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_saloons"
        android:name="in.zedone.bottomsample.ui.saloons.SaloonsFragment"
        android:label="@string/title_saloon"
        tools:layout="@layout/fragment_saloons" />

    <fragment
        android:id="@+id/navigation_offers"
        android:name="in.zedone.bottomsample.ui.offers.OffersFragment"
        android:label="@string/title_offer"
        tools:layout="@layout/fragment_offers" />

    <fragment
        android:id="@+id/navigation_account"
        android:name="in.zedone.bottomsample.ui.account.AccountFragment"
        android:label="@string/title_account"
        tools:layout="@layout/fragment_account" />

</navigation>
  1. 底部导航视图
<com.google.android.material.bottomnavigation.BottomNavigationView
     android:id="@+id/nav_view"
     android:layout_width="0dp"
     android:layout_height="wrap_content"
     android:layout_marginStart="0dp"
     android:layout_marginEnd="0dp"
     android:background="?android:attr/windowBackground"
     app:labelVisibilityMode="labeled"
     app:itemTextAppearanceActive="@style/BottomNavigationView.Active"
     app:itemTextAppearanceInactive="@style/BottomNavigationView"
     app:layout_constraintBottom_toBottomOf="parent"
     app:layout_constraintLeft_toLeftOf="parent"
     app:layout_constraintRight_toRightOf="parent"
     app:menu="@menu/bottom_nav_menu" />
  1. 主要活动
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_saloons, R.id.navigation_offers,R.id.navigation_account)
                .build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);

现在如何在底部导航中选择每个选项卡/片段时添加过渡/动画?

【问题讨论】:

  • 你找到办法了吗?根据您的问题,答案不正确,因为您没有操作。

标签: android navigation android-architecture-components bottomnavigationview


【解决方案1】:

不要使用 setupWithNavController 函数,而是按照这种方式。

首先,创建包含如下所示动画的 NavOptions。

val options = NavOptions.Builder()
            .setLaunchSingleTop(true)
            .setEnterAnim(R.anim.enter_from_bottom)
            .setExitAnim(R.anim.exit_to_top)
            .setPopEnterAnim(R.anim.enter_from_top)
            .setPopExitAnim(R.anim.exit_to_bottom)
            .setPopUpTo(navController.graph.startDestination, false)
            .build()

然后使用 setOnNavigationItemSelectedListener 来导航这样的动画。

bottomNavigationView.setOnNavigationItemSelectedListener { item ->
        when(item.itemId) {
            R.id.fragmentFirst -> {
                navController.navigate(R.id.fragmentFirst,null,options)
            }
            R.id.fragmentSecond -> {
                navController.navigate(R.id.fragmentSecond,null,options)
            }
            R.id.fragmentThird -> {
                navController.navigate(R.id.fragmentThird,null,options)
            }
        }
         true
    }

最后,您应该防止相同的项目选择情况,以便您可以添加以下代码。

bottomNavigationView.setOnNavigationItemReselectedListener { item ->
        return@setOnNavigationItemReselectedListener

我在我的项目中使用了bottomNavigation 来为页面过渡添加动画。 我希望它有所帮助。

【讨论】:

  • 这是我能找到的唯一简单且无错误的解决方案,完美运行,应该是公认的答案。请记住删除“setupwithnavcontroller”。谢谢!
  • 从哪里获得动画? “R.anim.enter_from”等都是红色的”
  • 嗨@Andrew,您可以将 anim 文件夹添加到 res 目录中,并通过 google 搜索添加您的动画,然后用您的名称更改名称。例如:enter_from_bottom.xml 应该是 schemas.android.com/apk/res/android" android:shareInterpolator="false">
【解决方案2】:

它还可以与 BottomNavigationView 的兄弟片段一起使用(JetPack 导航组件)

// FragmentA.kt
override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)

  exitTransition = MaterialFadeThrough()
}


// FragmentB.kt
override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)

  enterTransition = MaterialFadeThrough()
}

【讨论】:

  • 嗨,欢迎来到 StackOverflow。请提供有关您期望的解决方案的问题的更多详细信息。
【解决方案3】:

您也可以通过在每个Fragment 中覆盖onCreateAnimation() 并使用enter 参数检查您是否进入或退出Fragment 来保持setupWithNavController,然后创建适当的@ 987654327@ 和AnimationUtils.loadAnimation(context, animationId)

override fun onCreateAnimation(transit: Int, enter: Boolean, nextAnim: Int): Animation? {
    return if (enter) {
        AnimationUtils.loadAnimation(context, R.anim.fade_in)
    } else {
        AnimationUtils.loadAnimation(context, R.anim.fade_out)
    }
}

编辑作为对 iloo 的回应:

根据 Material Design (https://material.io/components/bottom-navigation#behavior),您一开始不应该这样做,但我想它仍然可以实现。

由于使用setupWithNavController 时,所有目的地都是顶级的,因此将 永远只是一个previousBackStackEntry,其目的地指向家 目的地,因此previousBackStackEntry 并不能帮助您确定您来自哪个Fragment

其他方法可能是在Activity 中有一个公共变量,您将在其中存储 您进入的最后一个目的地,并在恢复时在每个 Fragment 中设置该变量。 您可以在onAnimationCreate 中检查该变量以了解您来自哪个Fragment 并应用适当的动画。

【讨论】:

  • 对我来说,当动画不依赖于项目在底部导航菜单中的位置时,这种方法效果很好,相对于其他项目。淡入淡出有效,因为它没有方向。但是如果你想让视图从左或右滑入,向左或右滑出,取决于它的“在视图线上的位置”,传入的参数是不够的。可以详细说明这种方法以用于此类情况吗?
【解决方案4】:

我对 Android 导航有点陌生,但如果您想更改默认动画,我认为您可以尝试这样做

将这些文件放在你的动画目录中

nav_default_enter_anim.xml
nav_default_exit_anim.xml
nav_default_pop_enter_anim.xml
nav_default_pop_exit_anim.xml

交易的默认动画将更改为您在上述文件中放置的动画。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    相关资源
    最近更新 更多