片段不使用 FragmentContainerView 切换
Posted
技术标签:
【中文标题】片段不使用 FragmentContainerView 切换【英文标题】:Fragment not switching using FragmentContainerView 【发布时间】:2021-12-26 16:38:34 【问题描述】:我只是想将 bottomNavigation 与导航组件一起使用。我尝试了多种方法,但片段没有切换。只有底部导航显示的
我有一个使用片段而不是 FragmentContainerView 的正在运行的示例。但是我不知道为什么使用最新的技术太复杂了 仪表板活动
@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
<?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_
android:layout_>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_
android:layout_
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_
android:layout_
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_
app:menu="@menu/dashboard_nav_menu"
android:background="?android:attr/windowBackground"
android:layout_ />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
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"
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
<?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>
【问题讨论】:
【参考方案1】:问题不在于导航组件,您的代码看起来不错;但是您的主布局的大小为 0:
您需要设置根布局以匹配父级:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_
android:layout_>
...
旁注:片段将在工具栏后面绘制,因为它约束到父顶部边缘;底部也一样。
要将此约束修复到工具栏:
在FragmentContainerView
中替换:
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
与:
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
【讨论】:
还是不行 仍然没有显示任何片段?以上是关于片段不使用 FragmentContainerView 切换的主要内容,如果未能解决你的问题,请参考以下文章