首页 > 解决方案 > 欢迎屏幕没有显示,而是显示导航菜单?

问题描述

当我第一次启动应用程序时,我正在开发 android carrentup,它显示导航菜单而不是欢迎屏幕。我在用户启动应用程序时使用喷气背包导航,它必须在成功登录后显示欢迎屏幕,它必须在我的 mainactivity.kt 下方显示导航菜单

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

    private lateinit var appBarConfiguration: AppBarConfiguration
    lateinit var binding: ActivityMainBinding
    lateinit var navController: NavController

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)
        supportActionBar?.setBackgroundDrawable(ColorDrawable(resources.getColor(R.color.white)));

        navController = findNavController(R.id.nav_host_fragment)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        appBarConfiguration = AppBarConfiguration(navController.graph, binding.drawerLayout)
        setupActionBarWithNavController(navController, appBarConfiguration)
        binding.navView.setupWithNavController(navController)
        binding.navView.setNavigationItemSelectedListener(this)
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }


    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.cars_fragment -> {
                navController.navigate(R.id.action_global_carsMainFragment)
            }

            R.id.my_history_rides -> {
                navController.navigate(R.id.action_global_myHistoryRidesFragment)
            }

            R.id.email_us -> {

                navController.navigate(R.id.action_global_emailUsFragment)
            }
            R.id.information -> {
                navController.navigate(R.id.action_global_informationFragment)
            }


        }
        binding.drawerLayout.close()
        return false
    }

}

在我的 activity_main.xml 下面

<?xml version="1.0" encoding="utf-8"?>

<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_navigation_view"
        app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>

下面的navigation.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/navigation"
    app:startDestination="@id/carsMainFragment">

    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.digiandroidapp.ui.main.MainFragment"
        android:label="MainFragment">
        <action
            android:id="@+id/action_mainFragment_to_loginFragment"
            app:destination="@id/loginFragment"
            app:enterAnim="@anim/slide_in_from_right"
            app:exitAnim="@anim/slide_out_to_left"
            app:popEnterAnim="@anim/slide_in_from_left"
            app:popExitAnim="@anim/slide_out_to_right" />
        <action
            android:id="@+id/action_mainFragment_to_signUpFragment"
            app:destination="@id/signUpFragment"
            app:enterAnim="@anim/slide_in_from_right"
            app:exitAnim="@anim/slide_out_to_left"
            app:popEnterAnim="@anim/slide_in_from_left"
            app:popExitAnim="@anim/slide_out_to_right" />
        <action
            android:id="@+id/action_mainFragment_to_detailsFragment"
            app:destination="@id/detailsFragment" />
    </fragment>
    <fragment
        android:id="@+id/loginFragment"
        android:name="com.example.digiandroidapp.ui.login.LoginFragment"
        android:label="LoginFragment">
        <action
            android:id="@+id/action_loginFragment_to_signUpFragment"
            app:destination="@id/signUpFragment"
            app:enterAnim="@anim/slide_in_from_right"
            app:exitAnim="@anim/slide_out_to_left"
            app:popEnterAnim="@anim/slide_in_from_left"
            app:popExitAnim="@anim/slide_out_to_right" />
    </fragment>
    <fragment
        android:id="@+id/signUpFragment"
        android:name="com.example.digiandroidapp.ui.signup.SignUpFragment"
        android:label="SignUpFragment">
        <action
            android:id="@+id/action_signUpFragment_to_loginFragment"
            app:destination="@id/loginFragment"
            app:enterAnim="@anim/slide_in_from_right"
            app:exitAnim="@anim/slide_out_to_left"
            app:popEnterAnim="@anim/slide_in_from_left"
            app:popExitAnim="@anim/slide_out_to_right" />
    </fragment>
    <fragment
        android:id="@+id/carsMainFragment"
        android:name="com.example.digiandroidapp.ui.navigation_view.ui.cars.CarsMainFragment"
        android:label="Car Rental" >
        <action
            android:id="@+id/action_carsMainFragment_to_detailsFragment"
            app:destination="@id/detailsFragment" />
    </fragment>
    <action
        android:id="@+id/action_global_carsMainFragment"
        app:destination="@id/carsMainFragment"
        app:enterAnim="@anim/slide_in_from_right"
        app:exitAnim="@anim/slide_out_to_left"
        app:popEnterAnim="@anim/slide_in_from_left"
        app:popExitAnim="@anim/slide_out_to_right" />
    <fragment
        android:id="@+id/detailsFragment"
        android:name="com.example.digiandroidapp.ui.details.CarDetailsFragment"
        android:label="Car details"
        tools:layout="@layout/fragment_details" >
        <action
            android:id="@+id/action_detailsFragment_to_selectDateFragment"
            app:destination="@id/selectDateFragment" />
        <action
            android:id="@+id/action_detailsFragment_to_selectLocationFragment"
            app:destination="@id/selectLocationFragment" />
    </fragment>
    <fragment
        android:id="@+id/selectDateFragment"
        android:name="com.example.digiandroidapp.ui.details.date.SelectDateFragment"
        android:label="fragment_select_date"
        tools:layout="@layout/fragment_select_date" />
    <fragment
        android:id="@+id/selectLocationFragment"
        android:name="com.example.digiandroidapp.ui.details.location.SelectLocationFragment"
        android:label="fragment_select_location"
        tools:layout="@layout/fragment_select_location" />
    <fragment
        android:id="@+id/informationFragment"
        android:name="com.example.digiandroidapp.ui.navigation_view.ui.information.InformationFragment"
        android:label="Information"
        tools:layout="@layout/fragment_information" />
    <action android:id="@+id/action_global_informationFragment" app:destination="@id/informationFragment" />
    <fragment
        android:id="@+id/emailUsFragment"
        android:name="com.example.digiandroidapp.ui.navigation_view.ui.mail.EmailUsFragment"
        android:label="fragment_email_us"
        tools:layout="@layout/fragment_email_us" />
    <action android:id="@+id/action_global_emailUsFragment" app:destination="@id/emailUsFragment" />
    <fragment
        android:id="@+id/myHistoryRidesFragment"
        android:name="com.example.digiandroidapp.ui.navigation_view.ui.my_history_rides.MyHistoryRidesFragment"
        android:label="My Rides History"
        tools:layout="@layout/fragment_my_history_rides" /><action android:id="@+id/action_global_myHistoryRidesFragment" app:destination="@id/myHistoryRidesFragment"/>
</navigation>

在 MainFragment 下面

class MainFragment : Fragment() {

    private var _binding: WelcomeFragmentBinding? = null
    private val binding get() = _binding!!
    private val preferenceHelper by lazy { PreferenceHelper.defaultPreference(requireContext()) }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = WelcomeFragmentBinding.inflate(layoutInflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (preferenceHelper.token != "") {
            this.navigate(R.id.action_global_carsMainFragment)
        }
        navigate()
    }

    private fun navigate() {

        binding.login.setOnClickListener {
            findNavController().navigate(R.id.action_mainFragment_to_loginFragment)
        }

        binding.signUp.setOnClickListener {
            findNavController().navigate(R.id.action_mainFragment_to_signUpFragment)
        }
    }


    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

在welcome_fragment.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">

    <TextView
        android:id="@+id/digitalTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="80dp"
        android:fontFamily="@font/roboto_black"
        android:text="@string/digilab"
        android:textColor="@color/primaryBlack"
        android:textSize="48sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/rentCar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/rent_car_fo"
        android:textSize="18sp"
        android:fontFamily="@font/roboto_black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/digitalTitle" />

    <ImageView
        android:id="@+id/skodaImage"
        android:layout_width="260dp"
        android:layout_height="260dp"
        android:layout_marginTop="16dp"
        android:contentDescription="@string/email"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/rentCar"
        app:srcCompat="@drawable/image_skoda" />

    <Button
        android:id="@+id/login"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:text="@string/login"
        android:padding="16dp"
        android:fontFamily="@font/roboto_black"
        android:backgroundTint="@color/primaryButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/skodaImage" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/signUp"
        style="@style/Widget.MaterialComponents.Button.TextButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/white"
        android:fontFamily="@font/roboto_black"
        android:padding="16dp"
        android:text="@string/sign_up"
        android:textColor="@color/primaryButton"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/login"
        app:layout_constraintStart_toStartOf="@+id/login"
        app:layout_constraintTop_toBottomOf="@+id/login"
        app:layout_constraintVertical_bias="0.120000005" />
</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidkotlinandroid-fragmentsandroid-jetpack-navigation

解决方案


我错过了在导航中添加欢迎片段


推荐阅读