如何处理Android嵌套片段中的导航图

发布于 2025-01-28 18:11:21 字数 3255 浏览 5 评论 0原文

我正在片段中使用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 技术交流群。

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

发布评论

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

评论(1

余罪 2025-02-04 18:11:21

我认为您应该尝试此维护范围

(supportFragmentManager.findFragmentById(R.id.nav_menu_fragment) as NavHostFragment).navController.apply {
        binding.bnvAppBottomNav.setupWithNavController(this)
        addOnDestinationChangedListener { _, destination, _ ->
            binding.bnvAppBottomNav.visibility = when (destination.id) {
                R.id.Home,
                R.id.Wallet,
                R.id.Profile -> GONE

                else -> VISIBLE
            }
        }
    }

,也可以在res/layout/fragment_main_tab.xml中尝试一下

<androidx.fragment.app.FragmentContainerView
    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" />

,希望这给您一些提示

I think you should try this MainTabFragment

(supportFragmentManager.findFragmentById(R.id.nav_menu_fragment) as NavHostFragment).navController.apply {
        binding.bnvAppBottomNav.setupWithNavController(this)
        addOnDestinationChangedListener { _, destination, _ ->
            binding.bnvAppBottomNav.visibility = when (destination.id) {
                R.id.Home,
                R.id.Wallet,
                R.id.Profile -> GONE

                else -> VISIBLE
            }
        }
    }

And also try this in res/layout/fragment_main_tab.xml

<androidx.fragment.app.FragmentContainerView
    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" />

Hope this give you some hint

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文