NavigationView & Navigation #2

MainActivity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.neco_dev.navigationdrawerlesson.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
private lateinit var conf: AppBarConfiguration
private lateinit var navController: NavController
private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.ab.toolbar)
navController = findNavController(R.id.fragmentContainerView)
conf = AppBarConfiguration(
setOf(
R.id.item1,
R.id.item2
), binding.drawer
)
setupActionBarWithNavController(navController, conf)
binding.navView.setupWithNavController(navController)

}

override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp(conf) || super.onSupportNavigateUp()
}
}

main_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/main_navigation"
app:startDestination="@id/item1">

<fragment
android:id="@+id/item1"
android:name="com.neco_dev.navigationdrawerlesson.Item1Fragment"
android:label="fragment_item1"
tools:layout="@layout/fragment_item1" >
<action
android:id="@+id/action_item1Fragment_to_item2Fragment"
app:destination="@id/item2" />
</fragment>
<fragment
android:id="@+id/item2"
android:name="com.neco_dev.navigationdrawerlesson.Item2Fragment"
android:label="fragment_item2"
tools:layout="@layout/fragment_item2" >
<action
android:id="@+id/action_item2Fragment_to_item1Fragment"
app:destination="@id/item1" />
</fragment>
</navigation>

Зависимости для Navigation

implementation 'androidx.navigation:navigation-fragment-ktx:2.5.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.2'

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:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:id="@+id/fragmentContainerView"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/main_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity

Добавить комментарий

Ваш адрес email не будет опубликован.