android-fragments 单活动登录流程Android

pgccezyw  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(156)

我目前正在做一个学校项目,它会显示你的时间表、成绩、科目等。要使用Android应用程序中的任何功能,你必须登录。我的问题来了:
当用户第一次启动应用程序时,他们应该看到一个登录片段。一旦登录完成,他们将看到一个设置屏幕,在那里他们可以选择成绩和其他用户特定的东西的颜色主题。只有这样,他们应该看到实际的主片段。在第二次启动时,用户应该直接看到主片段
主片段是一个FragmentContainerView和一个BottomNavigationBar,其中包含5个其他片段。在每个子片段中,您可以单击项目。然后会出现一个不同的片段,显示更多的详细信息。但是这些片段会与底部导航栏重叠,因此您必须先向后导航,然后才能在底部导航栏中选择不同的片段。
就我所知,我需要嵌套的FragmentContainerViews

<?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应该托管LoginFragmentSetupFragment和一个MainFragment。在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作为MainFragment的子项,而MainFragment又是MainActivity的子项,则在HomeFragment中,用户单击一个按钮,例如SettingsFragment,这个片段应该显示为MainActivity的子项。我该如何让它工作呢?我已经看过这个问题,但并不真正了解如何实现它。How to setup Multiple nested FragmentContainerViews with respective navigation graphs?
有人可能会创建一个非常简单的实现这个。我特别需要帮助嵌套的FragmentContainerViewsBottomNavigationBarNavigationGraph。我也遇到了问题,底部的导航栏没有React了。感谢您的帮助提前。请让我知道,如果你需要更多的细节。

ar7v8xwq

ar7v8xwq1#

1.way

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.way
导航
https://www.youtube.com/watch?v=DI0NIk-7cz8&t=527s&ab_channel=Stevdza-San

相关问题