简体   繁体   English

片段不使用 FragmentContainerView 切换

[英]Fragment not switching using FragmentContainerView

I simply want to use bottomNavigation with Navigation Component .我只是想将bottomNavigation 与 Navigation Component一起使用。 I tried multiple ways but fragment not switching .我尝试了多种方法,但片段没有切换。 Only BottomNavigation display's只有底部导航显示的

I have a running sample that uses fragment instead of FragmentContainerView.我有一个正在运行的示例,它使用片段而不是 FragmentContainerView。 But I don't know why it's too complex to use the latest techtiques DashboardActivity但是不知道为什么使用最新的技术DashboardActivity太复杂了

@AndroidEntryPoint
class DashboardActivity : PBActivity(R.layout.activity_dashboard) {
    val binding: ActivityDashboardBinding by viewbind()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val appBarConfiguration: AppBarConfiguration = AppBarConfiguration.Builder(
            R.id.rankingFragment, R.id.homeFragment, R.id.profileFragment
        ).build()

        setSupportActionBar(binding.toolbar)
        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
        val navController = navHostFragment.navController
        /*val navView = findViewById<BottomNavigationView>(R.id.bottom_nav)
        NavigationUI.setupWithNavController(navView,navController)*/

        ///binding.navigationBb.setupWithNavController(navController)
        ///binding.toolbar.setupWithNavController(navController,null)
        //binding.navHostFragment.s
        binding.toolbar.setupWithNavController(navController,appBarConfiguration)
        ///binding.bottomNav.setupWithNavController(navController)

        NavigationUI.setupWithNavController(binding.bottomNav, navController)
        ///NavigationUI.setupWithNavController(binding.bottomNav,navController)
    }
 }

activity_dashboard.xml activity_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent">

        </androidx.appcompat.widget.Toolbar>

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/nav_graph" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_nav"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_width="match_parent"
            app:menu="@menu/dashboard_nav_menu"
            android:background="?android:attr/windowBackground"
            android:layout_height="wrap_content" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

nav_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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        tools:layout="@layout/fragment_home"
        android:name="uk.co.planetbeyond.game_show_app.presentation.ui.main.fragments.HomeFragment"
        android:label="HomeFragment" />
    <fragment
        android:id="@+id/rankingFragment"
        tools:layout="@layout/fragment_ranking"
        android:name="uk.co.planetbeyond.game_show_app.presentation.ui.main.fragments.RankingFragment"
        android:label="RankingFragment" />
    <fragment
        android:id="@+id/profileFragment"
        tools:layout="@layout/fragment_profile"
        android:name="uk.co.planetbeyond.game_show_app.presentation.ui.main.fragments.ProfileFragment"
        android:label="ProfileFragment" />
</navigation>

dashboard_nav_menu.xml dashboard_nav_menu.xml

 <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item
            android:id="@+id/rankingFragment"
            android:icon="@drawable/rankings_button_selected"
            android:title="Ranking" />
        <item
            android:id="@+id/homeFragment"
            android:icon="@drawable/home_button_selected"
            android:title="@string/title_home" />
        <item
            android:id="@+id/profileFragment"
            android:icon="@drawable/profile_icon_selected"
            android:title="Profile" />
    </menu>

The problem is not in the navigation components, your code looks fine regarding it;问题不在于导航组件,您的代码看起来不错; but that your main layout has a 0 size:但是您的主要布局的尺寸为 0:

You need to set the root layout to match parent:您需要设置根布局以匹配父级:

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
...

Side note: the fragments will draw behind the toolbar as it constraints to the parent top edge;旁注:片段将在工具栏后面绘制,因为它约束到父顶部边缘; The same for the bottom part.底部也一样。

To fix this constraint it to the toolbar:将此约束固定到工具栏:

In the FragmentContainerView Replace:FragmentContainerView替换:

app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"

With:和:

app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintBottom_toTopOf="@+id/bottom_nav"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误使用<fragmentcontainerview> ,但没有错误<fragment></fragment></fragmentcontainerview> - Error using <FragmentContainerView>, but no error with <fragment> 更换<fragment>使用 FragmentContainerView 标记 - Replace the <fragment> tag with FragmentContainerView 使用 androidx.fragment.app.FragmentContainerView 时出现 ClassNotFoundException - ClassNotFoundException when using androidx.fragment.app.FragmentContainerView 在 AndroidX FragmentContainerView 中处理 Fragment 的 BackPress - Handle BackPress for Fragment in AndroidX FragmentContainerView FragmentContainerView 使用 findNavController - FragmentContainerView using findNavController 使用带有导航组件的 FragmentContainerView? - Using FragmentContainerView with Navigation component? 添加到 FragmentContainerView 的视图必须与 Fragment 关联 - Views added to a FragmentContainerView must be associated with a Fragment 我可以从 Fragment 内部关闭 FragmentContainerView 吗? - Can I Close a FragmentContainerView from Inside a Fragment? Android R8 不保留 FragmentContainerView 中使用的片段 - Android R8 not keep fragment used in FragmentContainerView 从 MainActivity 获取 FragmentContainerView 中显示的当前片段 - Get current fragment displayed in FragmentContainerView from MainActivity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM