Хорошо.Основная проблема была с логической частью, и некоторые изменения пользовательского интерфейса должны были быть сделаны.Сначала просто войдите в свой файл content_main.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:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
Затем перейдите в свой файл 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_main"
app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
И, наконец, выполнитеизменения в основном в файле MainActivity.kt, например: ->
package com.example.parcaburada
import android.os.Bundle
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import androidx.drawerlayout.widget.DrawerLayout
import com.google.android.material.navigation.NavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import android.view.Menu
import android.view.MenuItem
import androidx.core.view.GravityCompat
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.ui.*
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
private lateinit var mToolbar: Toolbar
private lateinit var mDrawerLayout: DrawerLayout
private lateinit var mNavController: NavController
private lateinit var mNavigationView: NavigationView
private lateinit var mFloatingActionButton: FloatingActionButton
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setUpNavigation()
}
private fun setUpNavigation() {
//Toolbar
mToolbar = findViewById(R.id.toolbar)
setSupportActionBar(mToolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true)
//Toolbar
//Navigation Drawer
mDrawerLayout = findViewById(R.id.drawer_layout)
mNavigationView = findViewById(R.id.nav_view)
mNavController = Navigation.findNavController(this@MainActivity, R.id.nav_host_fragment)
NavigationUI.setupActionBarWithNavController(this@MainActivity, mNavController, mDrawerLayout)
NavigationUI.setupWithNavController(mNavigationView, mNavController)
mNavigationView.setNavigationItemSelectedListener(this)
//Navigation Drawer
//Floating Action button
mFloatingActionButton = findViewById(R.id.fab)
mFloatingActionButton.setOnClickListener { view ->
Snackbar.make(view, getString(R.string.text_fab_action), Snackbar.LENGTH_LONG)
.setAction(getString(R.string.text_action), null).show()
}
//Floating Action button
}
//Transition between fragments controller
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp(mNavController, mDrawerLayout)
}
//Navigation Drawer Controller
override fun onNavigationItemSelected(menuItem: MenuItem): Boolean {
menuItem.isChecked = true
mDrawerLayout.closeDrawers()
when (menuItem.itemId) {
R.id.nav_home -> navigateToFragment(R.id.nav_home)
R.id.nav_gallery -> navigateToFragment(R.id.nav_gallery)
R.id.nav_slideshow -> navigateToFragment(R.id.nav_slideshow)
R.id.nav_tools -> navigateToFragment(R.id.nav_tools)
R.id.nav_share -> navigateToFragment(R.id.nav_share)
R.id.nav_send -> navigateToFragment(R.id.nav_send)
}
return true
}
//Back button
override fun onBackPressed() {
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.END)
} else {
super.onBackPressed()
}
}
//Toolbar icon controller
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.shop -> navigateToFragment(R.id.nav_gallery)
}
return super.onOptionsItemSelected(item)
}
//Toolbar icon controller
//Custom method to navigate to fragments
private fun navigateToFragment(fragmentName: Int): Boolean {
mNavController.navigate(fragmentName)
return true
}
}
И готово.Все будет работать без сбоев.Спасибо.