gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:43:47 25 4
gpt4 key购买 nike

我已经成功地将底部导航与最新的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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com