我試圖僅在幾個片段中顯示底部欄導航(即隱藏我的大部分片段)..
我閱讀了有關此的官方檔案,但我不清楚,例如我將代碼放在哪里,并且我在 StackOverFlow 上閱讀了對此的評論,例如“我很驚訝檔案中建議這樣做,因為它實際上看起來很糟糕因為監聽器在新片段放置在螢屏上之前觸發,導致可見的閃爍(有時存在,有時不存在) “。
我對此進行了研究,據我了解,我需要創建一個嵌套的 naviagtion_graph (導航)或其他東西,聽 onDestinationChanged 并將底部導航或工具列設定為 Gone..
我不太明白這個..我只想隱藏大部分片段中的底部欄導航..
這是我的 navigation_gragh ( mobile_navigation) :
<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="com.example.testingbottomnav.ui.home.HomeFragment"
android:label="@string/title_home"
tools:layout="@layout/fragment_home" >
<action
android:id="@ id/action_navigation_home_to_newScreenFragment"
app:destination="@id/newScreenFragment" />
</fragment>
<fragment
android:id="@ id/navigation_dashboard"
android:name="com.example.testingbottomnav.ui.dashboard.DashboardFragment"
android:label="@string/title_dashboard"
tools:layout="@layout/fragment_dashboard" />
<fragment
android:id="@ id/navigation_notifications"
android:name="com.example.testingbottomnav.ui.notifications.NotificationsFragment"
android:label="@string/title_notifications"
tools:layout="@layout/fragment_notifications" />
<fragment
android:id="@ id/newScreenFragment"
android:name="com.example.testingbottomnav.ui.newscreen.NewScreenFragment"
android:label="fragment_new_screen"
tools:layout="@layout/fragment_new_screen" />
</navigation>
這是我的activity_main.xml:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@ id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<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:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:labelVisibilityMode="selected"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@ id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
這是我的MainActivty.kt:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val navView: BottomNavigationView = binding.navView
val navController = findNavController(R.id.nav_host_fragment_activity_main)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
}
任何幫助將不勝感激,謝謝..
uj5u.com熱心網友回復:
首先設定導航,然后將目的地更改偵聽器添加到導航控制器。每當您導航到片段navController.navigate(/* Some Destination Id */) 時,目標更改偵聽器就會在那里啟動,您可以檢查和比較是否要顯示或隱藏底部導航或工具列。
private fun setupBottomNavigation() {
val navHostFragment = supportFragmentManager
.findFragmentById(R.id.fragment_container_view) as NavHostFragment?
if (navHostFragment != null) {
navController = navHostFragment.navController
setupWithNavController(binding!!.navView, navController!!)
initDestinationListener()
}
}
private fun initDestinationListener() {
navController!!.addOnDestinationChangedListener { controller: NavController?, destination: NavDestination, bundle: Bundle? ->
// Showing or Hiding Bottom Navigation on Specific Screen
if (destination.id == R.id.navigation_plan ||
destination.id == R.id.navigation_video ||
destination.id == R.id.navigation_about ||
destination.id == R.id.navigation_detail ||
destination.id == R.id.navigation_contact ||
destination.id == R.id.navigation_privacy ||
destination.id == R.id.navigation_comments
) {
hideBottomNavigation()
} else {
showBottomNavigation()
}
}
}
uj5u.com熱心網友回復:
您可以撰寫一個方法來在您的活動中初始化導航并在此方法中隱藏導航并在 onCreate() 中使用它,如下所示:
private fun setupNavigation() {
val navController = findNavController(R.id.nav_host_fragment_activity_main)
navView.setupWithNavController(navController)
navController.addOnDestinationChangedListener { controller, destination, arguments ->
when (destination.id) {
R.id.fragment1 -> bottomNavigation.visibility = View.VISIBLE
R.id.fragment2 -> bottomNavigation.visibility = View.VISIBLE
R.id.fragment3 -> bottomNavigation.visibility = View.VISIBLE
else -> bottomNavigation.visibility = View.GONE
}
}
}
此代碼在片段 1 ,2 ,3 中顯示導航并將其隱藏到另一個片段
uj5u.com熱心網友回復:
我將這行代碼添加到 onCreate 方法中:
// hiding bottom bar
navController.addOnDestinationChangedListener { _, nd: NavDestination, _ ->
// the IDs of fragments as defined in the `navigation_graph`
if (nd.id == R.id.navigation_home || nd.id == R.id.navigation_dashboard
|| nd.id == R.id.navigation_notifications
) {
navView.visibility = View.VISIBLE
} else {
navView.visibility = View.GONE
}
}
uj5u.com熱心網友回復:
如果您只想BottomNavigationView在目的地是頂級目的地時顯示您的,您可以嘗試:
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home,
R.id.navigation_dashboard,
R.id.navigation_notifications
)
)
navController.addOnDestinationChangedListener { controller: NavController, destination: NavDestination, bundle: Bundle? ->
yourBottomNavigationView.isVisible = appBarConfiguration.topLevelDestinations.contains(destination.id)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/421233.html
標籤:
上一篇:CSS網格側欄將不包含子項
下一篇:在Fragment之間傳遞值
