我想使用新的导航图和BottomNavigationView
。我有以下依赖项:
// Navigation
implementation "androidx.navigation:navigation-fragment-ktx:$navVersion"
implementation "androidx.navigation:navigation-ui-ktx:$navVersion"
implementation "androidx.fragment:fragment:1.2.2"
// Material
implementation "com.google.android.material:material:$materialVersion"
请注意,implementation "androidx.fragment:fragment:1.2.2"
它应该解决FragmentContainerView
在导航图中未显示为主机的错误。
这是我的简单导航图:
您在这里看到的第一个奇怪的事情是,即使我添加了可以修复它的依赖项,也没有任何内容显示为主机。
我的主要活动布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?colorPrimary"
app:itemIconTint="@drawable/menu_navigation"
app:itemTextColor="@drawable/menu_navigation"
app:menu="@menu/menu_navigation" />
</LinearLayout>
和我的主要活动kotlin文件:
class MainActivity : AppCompatActivity() {
private var navController: NavController? = null
private var appBarConfiguration: AppBarConfiguration? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
navController =
(supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment).navController
appBarConfiguration = AppBarConfiguration.Builder(
setOf(
R.id.main_fragment,
R.id.recycler_fragment,
R.id.map_fragment
)
).build()
setupActionBar()
setupBottomNavigationMenu()
}
private fun setupActionBar() {
NavigationUI.setupActionBarWithNavController(
this,
navController!!,
appBarConfiguration!!
)
}
private fun setupBottomNavigationMenu() {
bottom_navigation?.let {
NavigationUI.setupWithNavController(it, navController!!)
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_toolbar, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val navigated = NavigationUI.onNavDestinationSelected(item, navController!!)
return navigated || super.onOptionsItemSelected(item)
}
}
这边还有菜单和工具栏:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="@+id/destination_home"
android:icon="@drawable/ic_home"
android:title="@string/home"
tools:layout="@layout/fragment_main" />
<item
android:id="@+id/destination_recycler"
android:icon="@drawable/ic_list"
android:title="@string/recycler"
tools:layout="@layout/fragment_recycler" />
<item
android:id="@+id/destination_map"
android:icon="@drawable/ic_map"
android:title="@string/map"
tools:layout="@layout/fragment_map" />
</menu>
<?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/destination_settings"
android:icon="@drawable/ic_settings"
android:title="@string/settings"
app:showAsAction="ifRoom" />
</menu>
当我现在启动应用程序时,我会期望以下行为:
What actually happens is:
So something is really messed up here with the navigation and I have no idea how to fix it.
Firstly, you are passing R.id.main_fragment, R.id.recycler_fragment, R.id.main_fragment
to your AppBarConfiguration
- besides including main_fragment
twice, those names don't match up with the destination_home
, destination_recycler
, and destination_map
in your menu XML. Since your clicking in your bottom nav bar works, those appear to be the correct destination IDs and you should change your AppBarConfiguration
to use the same IDs:
appBarConfiguration = AppBarConfiguration.Builder(
setOf(
R.id.destination_home,
R.id.destination_recycler,
R.id.destination_map
)
).build()
Or, if you're using the navigation-ui-ktx
artifact, you'd use the top level AppBarConfiguration
creator:
appBarConfiguration = AppBarConfiguration(setOf(
R.id.destination_home,
R.id.destination_recycler,
R.id.destination_map))
As per the onNavDestinationSelected()
Javadoc:
By default, the back stack will be popped back to the navigation graph's start destination. Menu items that have
android:menuCategory="secondary"
will not pop the back stack.
因此,可以预期的是,如果您在菜单中的“设置”屏幕中没有XML,则在您单击“后退”按钮时将使您返回到图形的起始目标。您想要更改菜单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/destination_settings"
android:icon="@drawable/ic_settings"
android:title="@string/settings"
app:showAsAction="ifRoom"
android:menuCategory="secondary" />
</menu>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句