当前位置:网站首页>Simple use of navigation in jetpack
Simple use of navigation in jetpack
2022-04-23 18:43:00 【KIDD-4869】
To configure
introduce Navigation( other Kotlin You need to introduce yourself , Then there are Kotlin The presentation of )
def nav_version = "2.3.2"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
The following is the project structure diagram , It's more clear to list , Although it is only a brief introduction to the use of . What is unfolded is where it can be used
Navigation
Next is the step-by-step operation , Direct column code will make people bigger , To be added later .
First, in the res Create files under folders
Fill in your name , choice Navigation, If you haven't created such a file , This folder will be automatically generated to store .
Next, write the generated file , I am here nav_main.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/nav_main"
// The initial... Defined here Fragment
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="com.kxqin.myapplication.fragment.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home">
// Jump is defined here , Using small arrows directly in the view will automatically generate
<action
android:id="@+id/action_homeFragment_to_secondFragment"
app:destination="@id/secondFragment"
// You can add your own animation
app:enterAnim="@anim/fragment_fade_enter"
app:exitAnim="@anim/fragment_fade_exit" />
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.kxqin.myapplication.fragment.SecondFragment"
android:label="fragment_second"
tools:layout="@layout/fragment_second" />
</navigation>
Use this button to quickly add components , Here is the visual operation platform , Well, don't feel low ah , In fact, it's very useful .
Using this arrow to pull it is a quick action Jump operation , But this action You need to define the specified operation in the subsequent code to take effect .( This is bullshit , This article does not do this operation .)
Next is the main page code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".activity.MainActivity"
android:orientation="vertical">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/nav_menu" />
<!-- here name Fixed to this class !-->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_main"
app:defaultNavHost="true" />
</LinearLayout>
We need to put a container FragmentContainerView To hold our Fragment,id There must be a definition of , Otherwise, an error will be reported when compiling .app:navGraph="@navigation/nav_main" It was just built Navigation The files under the .
I used BottomNavigationView, It's the bottom bar , I just put it on it .
Also generate a Menu Files of type , Here I define as nav_menu.xml.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/homeFragment"
android:title=" Home page " />
<item
android:id="@+id/secondFragment"
android:title=" The pie chart " />
</menu>
Be careful ! here item Of id And just now nav_main.xml Medium Fragment Of id Agreement , Otherwise, there will be no effect when clicking later !
Let's have a look MainActivity Code for
class MainActivity : AppCompatActivity() {
private val binding: ActivityMainBinding by viewbind()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
val appBarConfig = AppBarConfiguration.Builder(R.id.homeFragment, R.id.secondFragment).build()
// use fragment Container build navigation controller
val navController = (supportFragmentManager.findFragmentById(R.id.nav_host_fragment_container) as NavHostFragment).navController
// Set the configuration for the navigation controller
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfig)
// relation NavigationView And navigation controller
NavigationUI.setupWithNavController(binding.navView, navController)
}
}
Here we use ViewBinding, If you don't know, you can have a look Last article Oh , There is a simple introduction to use .
Click on tab You can switch Fragment La , Go and have a try !
版权声明
本文为[KIDD-4869]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210603464515.html
边栏推荐
- Setting up keil environment of GD single chip microcomputer
- Golang 语言实现TCP UDP通信
- RPM package management
- 教你用简单几个步骤快速重命名文件夹名
- K210串口通信
- Teach you to quickly rename folder names in a few simple steps
- 22年字节跳动飞书人力套件三面面经
- 机器学习理论基础篇--关于机器学习的一些术语
- QT notes on qmap container freeing memory
- Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
猜你喜欢
PyGame tank battle
Résolution: cnpm: impossible de charger le fichier... Cnpm. PS1 parce que l'exécution de scripts est désactivée sur ce système
Druid SQL和Security在美团点评的实践
解决:cnpm : 无法加载文件 ...\cnpm.ps1,因为在此系统上禁止运行脚本
[mathematical modeling] - analytic hierarchy process (AHP)
机器学习理论之(7):核函数 Kernels —— 一种帮助 SVM 实现非线性化决策边界的方式
CANopen STM32 transplantation
ESP32 LVGL8. 1 - label (style 14)
Custom prompt box MessageBox in QT
Excel intercept text
随机推荐
Daily network security certification test questions (April 14, 2022)
Domestic GD chip can filter
The first leg of the national tour of shengteng AI developer creation and enjoyment day was successfully held in Xi'an
ESP32 LVGL8. 1 - arc (arc 19)
C language simulates entering and leaving the stack, first in first out, first in first out, shared memory
Esp32 (UART 485 communication) - 485 communication of serial port (3)
MVVM model
Custom prompt box MessageBox in QT
Summary of actual business optimization scheme - main directory - continuous update
Usage of functions decode() and replace() in SQL
CISSP certified daily knowledge points (April 18, 2022)
Daily CISSP certification common mistakes (April 15, 2022)
Golang 语言实现TCP UDP通信
WiFi ap6212 driver transplantation and debugging analysis technical notes
使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
Nacos as service registry
STM32: LCD显示
QT notes on qmap container freeing memory
Get a list of recent apps
ctfshow-web362(SSTI)