android - 带有架构导航组件的底部导航 fragment 的动画

标签 android navigation android-architecture-components bottomnavigationview

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

  • 导航
  • <?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>
    
  • 底部导航查看
  • <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" />
    
  • 主要 Activity
  • 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);
    

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

    最佳答案

    不要使用 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 来为页面转换添加动画。
    我希望它有所帮助。

    关于android - 带有架构导航组件的底部导航 fragment 的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61541455/

    相关文章:

    导航 CSS :hover pushing content on Safari

    android - Worker 内部的 Synchronous 或 Asynchronous Rxjava(来自 WorkManager 组件)什么是正确的选择?

    单个 View 的 macOS SwiftUI 导航

    ios - UINavigationBar 不会扩展或扩展到设备宽度

    android - WorkerManager 是否是在应用程序中实现警报/提醒功能的可靠方式?

    安卓 MVVM : How to subscribe to a network call by LiveData in ViewModel

    Android 中心约束

    java - 如何使用 "departure_time"作为参数通过距离矩阵 api 将 future 时间传递到 HTTP 请求中

    android - 如何在非 root 手机上查看我的应用程序的数据库

    android - 如何使用多个 res.srcDirs 并用 gradle 覆盖一些资源