gpt4 book ai didi

android - 如何在Android上的NavigationUI组件中动态更改默认选项卡

转载 作者:行者123 更新时间:2023-12-02 13:16:42 25 4
gpt4 key购买 nike

在我的应用程序中,我想显示底部的选项卡,单击这些选项卡时显示一个fragment
为此,我使用BottomNavigationViewNavigationUI组件显示片段
我的XML代码:

<fragment
android:id="@+id/homePage_fragmentNavHost"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/home_navigator"
app:defaultNavHost="true"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@+id/homePage_bottomNavBar"
app:layout_constraintTop_toBottomOf="@+id/homePage_toolbar"/>
<!--Bottom menu-->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/homePage_bottomNavBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:background="?android:attr/windowBackground"
app:menu="@menu/menu_home_navigation"
app:labelVisibilityMode="selected"
app:itemTextAppearanceActive="@style/BottomNavigationView.Active"
app:itemTextAppearanceInactive="@style/BottomNavigationView"
app:itemTextColor="@color/bottom_nav_bar_colors"
app:itemIconTint="@color/bottom_nav_bar_colors"/>
我在代码下面编写 ,用于连接NavigationUiBottomNavigationView:
private fun setupNavigation() {
val navController = Navigation.findNavController(this, R.id.homePage_fragmentNavHost)
NavigationUI.setupWithNavController(homePage_bottomNavBar, navController)
}

override fun onSupportNavigateUp() = Navigation.findNavController(this, R.id.homePage_fragmentNavHost).navigateUp()
但请始终在默认选项卡上显示项目0。
我要写入条件并检查一个值,并为此值设置NavigationUi的默认选项默认选项卡
我该怎么做?

最佳答案

graph中,您可以定义startDestination
就像是:

    val navHostFragment = supportFragmentManager.findFragmentById(R.id.homePage_fragmentNavHost) as NavHostFragment
val graphInflater = navHostFragment.navController.navInflater
val navGraph = graphInflater.inflate(R.navigation.home_navigator)
navGraph.startDestination = R.id.nav_xxxxx
navController.graph = navGraph


findViewById<BottomNavigationView>(R.id.homePage_bottomNavBar)
.setupWithNavController(navController)

关于android - 如何在Android上的NavigationUI组件中动态更改默认选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62911255/

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