当前位置:网站首页>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
边栏推荐
猜你喜欢
ESP32 LVGL8. 1 - arc (arc 19)
昇腾 AI 开发者创享日全国巡回首站在西安成功举行
ctfshow-web362(SSTI)
机器学习理论之(8):模型集成 Ensemble Learning
从技术体系到商业洞察,中小研发团队架构实践之收尾篇
Query the logistics update quantity according to the express order number
ctfshow-web362(SSTI)
ctfshow-web361(SSTI)
ctfshow-web361(SSTI)
Machine learning theory (7): kernel function kernels -- a way to help SVM realize nonlinear decision boundary
随机推荐
使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
Esp32 drive encoder -- siq-02fvs3 (vscade + IDF)
视频边框背景如何虚化,简单操作几步实现
机器学习实战 -朴素贝叶斯
Iptables - L executes slowly
Ionic 从创建到打包指令集顺序
Daily network security certification test questions (April 18, 2022)
Screenshot using projectmediamanager
Test questions of daily safety network (February 2024)
Actual combat of Nacos as service configuration center
With the use of qchart, the final UI interface can be realized. The control of qweight can be added and promoted to a user-defined class. Only the class needs to be promoted to realize the coordinate
MVVM模型
After CANopen starts PDO timing transmission, the heartbeat frame time is wrong, PDO is delayed, and CANopen time axis is disordered
ESP32 LVGL8. 1 - calendar (calendar 25)
ESP32 LVGL8. 1 - label (style 14)
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
Const keyword, variable and function are decorated with const
配置iptables
QT excel operation summary
CANopen usage method and main parameters of object dictionary