当前位置:网站首页>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)
}
边栏推荐
- Time unified system
- VR全景制作的优势是什么?为什么能得到青睐?
- Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
- Report on operation pattern and future prospect of global and Chinese propyl isovalerate industry from 2022 to 2028
- Current situation analysis and development trend forecast report of global and Chinese acrylonitrile butadiene styrene industry from 2022 to 2028
- 传输层 以字节为单位的滑动窗口技术
- One way 和two way ANOVA分析的区别是啥,以及如何使用SPSS或者prism进行统计分析
- Why are life science enterprises on the cloud in succession?
- What are the advantages of VR panoramic production? Why is it favored?
- 颜色渐变梯度颜色集合
猜你喜欢

人体改造 VS 数字化身

【面试题】什么是事务,什么是脏读、不可重复读、幻读,以及MySQL的几种事务隔离级别的应对方法

JDBC - database connection

A small program written this week

Signal integrity (SI) power integrity (PI) learning notes (XXV) differential pair and differential impedance (V)

U.S. House of Representatives: digital dollar will support the U.S. dollar as the global reserve currency

Interesting checkbox counters

技术分享| WVP+ZLMediaKit实现摄像头GB28181推流播放

Human body transformation vs digital Avatar

无人驾驶: 对多传感器融合的一些思考
随机推荐
【面试题】什么是事务,什么是脏读、不可重复读、幻读,以及MySQL的几种事务隔离级别的应对方法
svg线条动画背景js特效
How to delete the entire row with duplicate items in a column of WPS table
C WinForm maximizes occlusion of the taskbar and full screen display
【图数据库性能和场景测试利器LDBC SNB】系列一:数据生成器简介 & 应用于GES服务
离散数学及其应用 2018-2019学年春夏学期期末考试 习题详解
Time unified system
Tutorial details | how to edit and set the navigation function in the coolman system?
Intensive reading of thinking about markdown
[interview question] the difference between instancof and getclass()
Opengauss kernel: simple query execution
[figure database performance and scenario test sharp tool ldbc SNB] series I: introduction to data generator & Application to ges service
怎么把wps表格里某一列有重复项的整行删掉
Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
Some examples of MgO operating database in go
时间统一系统
颜色渐变梯度颜色集合
Hibernate learning 2 - lazy loading (delayed loading), dynamic SQL parameters, caching
After 5 years of software testing in didi and ByteDance, it's too real
为什么生命科学企业都在陆续上云?






