当前位置:网站首页>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
边栏推荐
- Feature selection feature_ selection--SelectKBest
- iptables -L执行缓慢
- K210 serial communication
- Halo 开源项目学习(七):缓存机制
- QT curve / oscilloscope customplot control
- 机器学习理论之(7):核函数 Kernels —— 一种帮助 SVM 实现非线性化决策边界的方式
- Esp32 drive encoder -- siq-02fvs3 (vscade + IDF)
- Machine learning theory (7): kernel function kernels -- a way to help SVM realize nonlinear decision boundary
- How to virtualize the video frame and background is realized in a few simple steps
- QT error: no matching member function for call to ‘connect‘
猜你喜欢
listener.log
Excel intercept text
Function recursion and solving interesting problems
Introduction to ROS learning notes (II)
Introduction to ROS learning notes (I)
Tangle
使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
Teach you to quickly rename folder names in a few simple steps
使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
Creation and use of QT dynamic link library
随机推荐
Use Chenxi bookkeeping book to analyze the balance of revenue and expenditure of each account in a certain period of time
Nacos as service registry
Practice of Druid SQL and security in meituan review
Function recursion and solving interesting problems
Machine learning theory (8): model integration ensemble learning
Druid SQL和Security在美团点评的实践
Screenshot using projectmediamanager
CISSP certified daily knowledge points (April 18, 2022)
STM32: LCD显示
数据库上机实验四(数据完整性与存储过程)
SQL中函数 decode()与 replace()的用法
Promote QT default control to custom control
Introduction to quantexa CDI syneo platform
Introduction to ROS learning notes (II)
Daily network security certification test questions (April 15, 2022)
Const keyword, variable and function are decorated with const
Chondroitin sulfate in vitreous
【数学建模】—— 层次分析法(AHP)
PyGame tank battle
[mathematical modeling] - analytic hierarchy process (AHP)