如何处理Android嵌套片段中的导航图
我正在片段中使用bottomNavigationView
,并希望在每个选项上显示不同的片段。我可以看到BottomNavigationView
,但我无法加载任何片段。我正在使用以下代码。
res/layout/fragment_main_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<fragment
android:id="@+id/nav_menu_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/main_menu_nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="@drawable/bottom_view_selector"
app:labelVisibilityMode="labeled"
app:layout_anchorGravity="bottom"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/bottom_nav_menu"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
res/navigation/main_menu_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"
android:id="@+id/main_menu_nav_graph"
app:startDestination="@id/navigation_main_tab_fragment">
<fragment
android:id="@+id/navigation_main_tab_fragment"
android:name="com.pkg.ui.main.MainTabFragment"
android:label="@string/app_name"/>
</navigation>
code> maintabfragment.kt
binding.bottomNavigationView.setOnItemSelectedListener {
when (it.itemId) {
R.id.home -> {
findNavController().navigate(R.id.nav_menu_to_main_fragment)
}
R.id.wallet -> {
findNavController().navigate(R.id.nav_menu_to_main_fragment)
}
else -> {
null
}
}
true
}
res/navigation/navigation/navigation/navigation/nav_menu_graph.xml.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"
android:id="@+id/nav_menu_graph"
app:startDestination="@id/nav_menu_to_main_fragment">
<fragment
android:id="@+id/nav_menu_to_main_fragment"
android:name="com.pkg.ui.main.MainFragment"
android:label="@string/app_name" />
</navigation>
它总是显示空白,没有mainfragment
或崩溃。有人可以检查一次并突出显示我是否犯错?提前致谢。
I'm using BottomNavigationView
in a fragment and want to show different fragments on tap of each option og that. I'm able to see BottomNavigationView
but I'm unable to load any fragment. I'm using below code for that.
res/layout/fragment_main_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<fragment
android:id="@+id/nav_menu_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/main_menu_nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="@drawable/bottom_view_selector"
app:labelVisibilityMode="labeled"
app:layout_anchorGravity="bottom"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/bottom_nav_menu"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
res/navigation/main_menu_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"
android:id="@+id/main_menu_nav_graph"
app:startDestination="@id/navigation_main_tab_fragment">
<fragment
android:id="@+id/navigation_main_tab_fragment"
android:name="com.pkg.ui.main.MainTabFragment"
android:label="@string/app_name"/>
</navigation>
MainTabFragment.kt
binding.bottomNavigationView.setOnItemSelectedListener {
when (it.itemId) {
R.id.home -> {
findNavController().navigate(R.id.nav_menu_to_main_fragment)
}
R.id.wallet -> {
findNavController().navigate(R.id.nav_menu_to_main_fragment)
}
else -> {
null
}
}
true
}
res/navigation/nav_menu_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"
android:id="@+id/nav_menu_graph"
app:startDestination="@id/nav_menu_to_main_fragment">
<fragment
android:id="@+id/nav_menu_to_main_fragment"
android:name="com.pkg.ui.main.MainFragment"
android:label="@string/app_name" />
</navigation>
It always shows blank without MainFragment
or either it gets crashed. Can anyone please check once and highlight if I'm making any mistake? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您应该尝试此维护范围
,也可以在res/layout/fragment_main_tab.xml中尝试一下
,希望这给您一些提示
I think you should try this MainTabFragment
And also try this in res/layout/fragment_main_tab.xml
Hope this give you some hint