gpt4 book ai didi

android - BottomNavigationView.setupWithNavController 不工作

转载 作者:行者123 更新时间:2023-12-04 14:05:40 28 4
gpt4 key购买 nike

目前我正在开发一个 Android 应用程序,我想在其中同时使用 Android Navigation ComponentBottomNavigationView .
在开发上述应用程序时,我查看了 官方codelab 还有几个问题,但事实证明它们没有任何帮助。
问题是分配 navController给我的BottomNavigationView似乎没有任何效果——点击菜单项不会影响导航主机。
我的代码:
main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayoutxmlns: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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/main_host_fr"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_nav_bnv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@android:color/transparent"
android:elevation="0dp"
app:elevation="0dp"
app:menu="@menu/menu_main" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
MainActivity.kt
import android.os.Bundle
import android.os.PersistableBundle
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.setupWithNavController
import com.aredruss.qurio.databinding.ActivityMainBinding
import com.aredruss.qurio.helpers.viewBinding

class MainActivity : AppCompatActivity() {

// ViewBindingDelegate
private val binding: ActivityMainBinding by viewBinding(ActivityMainBinding::inflate)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

}

// Using onPostCreate because accessing NavHost in OnCreate causes crashes
override fun onPostCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onPostCreate(savedInstanceState, persistentState)
binding.navigationBb.setupWithNavController(findNavController(R.id.main_host_fr))
NavigationUI.setupWithNavController(binding.mainNavBnv, findNavController(R.id.main_host_fr))
}
}
nav_graph.xml
<?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/nav_graph"
app:startDestination="@id/action_history">

<fragment
android:id="@+id/action_history"
android:name="com.aredruss.qurio.ui.HistoryFragment"
android:label="HistoryFragment"
tools:layout="@layout/fragment_history" >
</fragment>

<fragment
android:id="@+id/action_settings"
android:name="com.aredruss.qurio.ui.SettingsFragment"
android:label="SettingsFragment"
tools:layout="@layout/fragment_settings" >
</fragment>
</navigation>
menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_history"
android:icon="@drawable/ic_history"
android:title="History"
app:showAsAction="withText" />

<item
android:id="@+id/action_settings"
android:title="Settings"
android:icon="@drawable/ic_settings"
app:showAsAction="withText"
/>
</menu>
我已经 checkout this解决方案,它没有任何帮助。

最佳答案

你覆盖了错误的onPostCreate() :

override fun onPostCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
你不想要带 PersistableBundle 的那个。 - 你想要普通的:
override fun onPostCreate(savedInstanceState: Bundle?) {
现在您的 onPostCreate()实际上会被调用。
当然,你不应该使用 onPostCreate()一点也不。相反,您应该是 following the documentation ,其中明确指出您应该使用以下代码来获取您的 NavControlleronCreate() :
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.main_host_fr) as NavHostFragment
val navController = navHostFragment.navController
这使您可以将整个代码编写为:
// ViewBindingDelegate
private val binding: ActivityMainBinding by viewBinding(ActivityMainBinding::inflate)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Note how we actually set the content view from the binding
setContentView(binding.root)

val navHostFragment =
supportFragmentManager.findFragmentById(R.id.main_host_fr) as NavHostFragment
val navController = navHostFragment.navController

binding.navigationBb.setupWithNavController(navController)
binding.mainNavBnv.setupWithNavController(navController)
}

关于android - BottomNavigationView.setupWithNavController 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68602841/

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