单一活动登录流Android



我目前正在做一个学校项目,该项目显示你的时间表、成绩、科目等。要使用Android应用程序中的任何功能,你必须登录。我的问题来了:

当用户第一次启动应用程序时,他们应该看到一个登录片段。登录完成后,他们将看到一个设置屏幕,在那里他们可以为成绩和其他用户特定的东西选择颜色主题。只有到那时,他们才能看到真正的主要片段。在第二次启动时,用户应该直接看到主片段

主要片段是FragmentContainerView和承载5个其他片段的BottomNavigationBar。在每个子页面中,您都可以单击项目。然后应该呈现一个不同的片段,显示更多的细节。但是,这些片段应该与底部导航重叠,因此在底部导航栏中选择不同的片段之前,您必须导航回。

据我所知,我需要嵌套的FragmentContainerViewsMainActivity应为

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity应当承载LoginFragmentSetupFragmentMainFragment。在MainFragment应该像这个

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>

我想通过一个导航图来实现所有这些。对于每个FragmentContainerView,我必须使用一张图还是两张图?然后还有细节碎片的问题。例如,如果我有一个HomeFragment作为MainActivity的子级MainFragment的子级,并且在HomeFragment中用户点击例如SettingsFragment的按钮,则该片段应该显示为MainActivity的子级。我该怎么做?我已经看了这个问题,但并不真正理解如何实现它。如何设置具有相应导航图的多个嵌套FragmentContainerViews?

也许有人可以创建一个非常简单的实现。我特别需要关于嵌套的FragmentContainerViewsBottomNavigationBarNavigationGraph的帮助。我还遇到了底部导航栏不再响应的问题。感谢您提前提供的帮助。如果你需要更多的细节,请告诉我。

1路

val homeFragment = HomeFragment()
val listFragment = ListFragment()
val profileFragment = ProfileFragment()
nav_view.setOnItemSelectedListener {
when (it) {
R.id.homeFragment -> setCurrentFragment(homeFragment)
R.id.listFragment -> setCurrentFragment(listFragment)
R.id.profileFragment -> 
setCurrentFragment(profileFragment)
}
return@setOnItemSelectedListener
}

private fun setCurrentFragment(fragment: Fragment) =
supportFragmentManager.beginTransaction().apply {
replace(R.id.nav_host_fragment_activity_main, fragment)
addToBackStack(null) //If you delete this, press the back button, it will not return to the previous fragment.
commit()
}

2路

导航

https://www.youtube.com/watch?v=DI0NIk-7cz8&t=527s&ab_channel=圣大街

最新更新