底部导航视图不适用于导航组件

发布于 2025-01-15 14:32:30 字数 3570 浏览 6 评论 0原文

我正在使用带有导航组件的底部导航视图。我在此应用程序中使用了嵌套导航主机,选择了底部导航菜单但不显示其片段。我遵循一些代码,但它不起作用。 我试过这段代码 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻身的咸鱼 2025-01-22 14:32:30
Initialize BottomNavigationView and Navigation controller in your Activity and merge them.

Suppose Your Activity Name "SecondActivity".

class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        val bottomNaviagtionview: BottomNavigationView = findViewById(R.id.bottomNavigationView)
        val navController: NavController = findNavController(R.id.fragment)
        bottomNaviagtionview.setupWithNavController(navController)
    }
}
Initialize BottomNavigationView and Navigation controller in your Activity and merge them.

Suppose Your Activity Name "SecondActivity".

class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        val bottomNaviagtionview: BottomNavigationView = findViewById(R.id.bottomNavigationView)
        val navController: NavController = findNavController(R.id.fragment)
        bottomNaviagtionview.setupWithNavController(navController)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文