当前位置:网站首页>Use of navigation and navigationui
Use of navigation and navigationui
2022-06-25 00:21:00 【BY-91】
List of articles
rely on
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.1'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.1'
navigation The main elements of
- Navigation Graph A kind of xml Resource file , Contains all the pages of the app and the relationships between them , That is to say fragment The place where you put it ,
- NavHostFragement A special kind fragment, It can be understood as fragment The container of , yes fragment Exhibition UI The place of ,Navigation Graph Medium fragment adopt NavHostFragement Exhibition
- NavController controller , Used to complete... In code Navigation Graph Specific page switching work in
Navigation Graph The creation of
res Folder -new - Android Resource File, newly build Navigation Graph file .
add to NavHostFragment
NavHostFragment It's a special one Fragment, Add to activity In the layout of .
- Created nav_graph File after the introduction of , And tell the system that this is a special fragment-NavHostFragment
- app:defaultNavHos
- app:defaultNavHost=“true” Automatic processing system return key
- navGraph attribute Set the Fragment The corresponding navigation map -
<fragment
android:id="@+id/nav_host_fragment_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"/>
establish destination
Specify the default first page
app:startDestination The first thing to show fragment
Specify the page you want to go to
NavController Complete navigation and pass parameters
first fragment Click to jump and pass parameters
var view = inflater.inflate(R.layout.fragment_main, container, false)
var bundle = Bundle()
bundle.putString("user","Bliss")
bundle.putInt("age",30)
Navigation.findNavController(view).navigate(R.id.action_mainFragment_to_secondFragment,bundle)
// Method 2 Navigation.createNavigateOnClickListener(R.id.action_mainFragment_to_secondFragment)
Add page switch animation
Create animation files , This standard document is also available on the official website , Page switch animation
slide_in_left file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-50%p" android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
slide_in_right file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="50%p" android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
slide_out_left file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-50%p"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
slide_out_right file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="50%p"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
Introducing animation
<fragment
android:id="@+id/mainFragment"
android:name="com.bliss.yang.jetapp.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main">
<action
android:id="@+id/action_mainFragment_to_secondFragment"
app:destination="@id/secondFragment"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right" />
Use safe args Plug in parameters
rely on
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.3"//safe-args Plug in parameters
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'androidx.navigation.safeargs' // app Of build.gradle Introduce added dependencies in
}
The ginseng
var bundle = MainFragmentArgs.Builder()
.setUser("Bliss91")
.setAge(31).build().toBundle()
Navigation.findNavController(view).navigate(R.id.action_mainFragment_to_secondFragment,bundle)
Accept parameters
if(bundle!=null){
var user = MainFragmentArgs.fromBundle(bundle).user
var age = MainFragmentArgs.fromBundle(bundle).age
Log.e(TAG, "safe-args Receiving parameters :$user,$age " )
}

<fragment
android:id="@+id/mainFragment"
android:name="com.bliss.yang.jetapp.MainFragment"
android:label="fragment_main"
tools:layout="@layout/fragment_main">
<action
android:id="@+id/action_mainFragment_to_secondFragment"
app:destination="@id/secondFragment"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right" />
<argument
android:name="user"
app:argType="string"
android:defaultValue="" />
<argument
android:name="age"
app:argType="integer"
android:defaultValue="0" />
</fragment>
NavigationUI Use
Make similar to APP bar The buttons and menus in are associated with the navigation page
app bar menu
menu_setting
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Here id Want to be with nav_graph Keep the same in -->
<item
android:id="@+id/secondFragment"
android:icon="@drawable/ic_launcher_foreground"
android:title=" Set up ">
</item>
</menu>

activity Instantiate the menu bar in
// Add menu bar
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
super.onCreateOptionsMenu(menu)
menuInflater.inflate(R.menu.menu_setting,menu)
return true
}
Handling jump logic
private var appBarConfiguration:AppBarConfiguration?=null// be used for APPBar Configuration of
private var navController:NavController?=null// For navigation and switching of pages
navController = Navigation.findNavController(this,R.id.nav_host_fragment_container)
navController?.let { _navController ->
appBarConfiguration = AppBarConfiguration.Builder(_navController.graph).build()
}
// take APPBar and navController binding
if (navController!=null && appBarConfiguration!=null){
NavigationUI.setupActionBarWithNavController(this, navController!!,
appBarConfiguration!!
)
}
if (navController!=null){
navController?.addOnDestinationChangedListener { controller, destination, arguments ->
Log.e(TAG, "onDestinationChanged: Page switching monitoring ")
}
}
* adopt NavigationUI Complete the click jump
*/
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (navController!=null){
return NavigationUI.onNavDestinationSelected(item, navController!!)
}
return super.onOptionsItemSelected(item)
}
/**
* Use NavigationUI Complete return
*/
override fun onSupportNavigateUp(): Boolean {
if (navController!=null && appBarConfiguration!=null){
return NavigationUI.navigateUp(navController!!, appBarConfiguration!!)
}
return super.onSupportNavigateUp()
}
because menu Is written in the book activity in , So the second one fragment Clean it up in the middle of the game menu The difference effect
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
menu.clear()// Clear the parent container activity Medium menu,
super.onCreateOptionsMenu(menu, inflater)
}
边栏推荐
- Human body transformation vs digital Avatar
- Fast pace? high pressure? VR panoramic Inn brings you a comfortable life
- VR全景怎么赚钱?结合市场从两个方面客观分析下
- Tutorial details | how to edit and set the navigation function in the coolman system?
- Some examples of MgO operating database in go
- Current situation analysis and development trend prediction report of hesperidase industry in the world and China from 2022 to 2028
- On the difficulty of developing large im instant messaging system
- Paper review: U2 net, u-net composed of u-net
- Fuxin Kunpeng joins in, and dragon lizard community welcomes a new partner in format document technical service
- 【Proteus仿真】定时器0作为16位计数器使用示例
猜你喜欢

节奏快?压力大?VR全景客栈带你体验安逸生活
5-minute NLP: summary of 3 pre training libraries for rapid realization of NER

微搭低代码中实现增删改查

无人驾驶: 对多传感器融合的一些思考

在滴滴和字节跳动干了 5年软件测试,太真实…

颜色渐变梯度颜色集合
Is it so difficult to calculate the REM size of the web page according to the design draft?

svg线条动画背景js特效

∞ symbol line animation canvasjs special effect

【面试题】什么是事务,什么是脏读、不可重复读、幻读,以及MySQL的几种事务隔离级别的应对方法
随机推荐
Canvas spiral style animation JS special effect
【排行榜】Carla leaderboard 排行榜 运行与参与手把手教学
Interesting checkbox counters
Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
[figure database performance and scenario test sharp tool ldbc SNB] series I: introduction to data generator & Application to ges service
Report on operation pattern and future prospect of global and Chinese propyl isovalerate industry from 2022 to 2028
Current situation analysis and development trend prediction report of hesperidase industry in the world and China from 2022 to 2028
Analysis report on operation trend and investment strategy of global and Chinese tetrahydrofurfuryl propionate industry from 2022 to 2028
Zed acquisition
[Solved] Public key for mysql-community-xxx. rpm is not installed
干接点和湿接点
The new employee of the Department after 00 is really a champion. He has worked for less than two years. The starting salary of 18K is close to me when he changes to our company
I suddenly find that the request dependent package in NPM has been discarded. What should I do?
Basic summary of MySQL database knowledge
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it
U.S. House of Representatives: digital dollar will support the U.S. dollar as the global reserve currency
Adding, deleting, modifying and checking in low build code
∞符号线条动画canvasjs特效
Collection of software testing and game testing articles
为什么生命科学企业都在陆续上云?






