底部导航视图不适用于导航组件
我正在使用带有导航组件的底部导航视图。我在此应用程序中使用了嵌套导航主机,选择了底部导航菜单但不显示其片段。我遵循一些代码,但它不起作用。 我试过这段代码 MainFragment.ktfragment_main.xmlbottom_menu.xmlbottom_nav_graph.xml
class MainFragment : BaseFragment<FragmentMainBinding>(FragmentMainBinding::inflate) {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return super.onCreateView(inflater, container, savedInstanceState)
val navController = findNavController(fragmentBinding.fragment)
fragmentBinding.bottomNavigationView.setupWithNavController(navController)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
}
片段
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.MainFragment">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment"
android:name="com.newroz.myapplication.view.HomeFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:navGraph="@navigation/bottom_nav_graph"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
的
<?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/homeFragment"
android:title="Home"
android:icon="@drawable/ic_baseline_home_24"/>
<item
android:id="@+id/allUserFragment"
android:title="All User"
android:icon="@drawable/ic_baseline_people_24"/>
<item
android:id="@+id/profileFragment"
android:title="Profile"
android:icon="@drawable/ic_baseline_settings_24"/>
?
<?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/bottom_nav_graph"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.newroz.myapplication.view.HomeFragment"
android:label="HomeFragment" />
<fragment
android:id="@+id/allUserFragment"
android:name="com.newroz.myapplication.view.AllUserFragment"
android:label="AllUserFragment" />
<fragment
android:id="@+id/profileFragment"
android:name="com.newroz.myapplication.view.ProfileFragment"
android:label="ProfileFragment" />
</navigation>
如何显示单击 谢谢
I am using bottom navigation view with navigation component. I used nested navhost in this app, the bottom navigation menu is selected but do not show their fragment. I follow some code but it doesn't work.
I tried this code
MainFragment.kt
class MainFragment : BaseFragment<FragmentMainBinding>(FragmentMainBinding::inflate) {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return super.onCreateView(inflater, container, savedInstanceState)
val navController = findNavController(fragmentBinding.fragment)
fragmentBinding.bottomNavigationView.setupWithNavController(navController)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
}
fragment_main.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.MainFragment">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment"
android:name="com.newroz.myapplication.view.HomeFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:navGraph="@navigation/bottom_nav_graph"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
bottom_menu.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/homeFragment"
android:title="Home"
android:icon="@drawable/ic_baseline_home_24"/>
<item
android:id="@+id/allUserFragment"
android:title="All User"
android:icon="@drawable/ic_baseline_people_24"/>
<item
android:id="@+id/profileFragment"
android:title="Profile"
android:icon="@drawable/ic_baseline_settings_24"/>
bottom_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/bottom_nav_graph"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.newroz.myapplication.view.HomeFragment"
android:label="HomeFragment" />
<fragment
android:id="@+id/allUserFragment"
android:name="com.newroz.myapplication.view.AllUserFragment"
android:label="AllUserFragment" />
<fragment
android:id="@+id/profileFragment"
android:name="com.newroz.myapplication.view.ProfileFragment"
android:label="ProfileFragment" />
</navigation>
how can i show clicked fragment?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)