当前位置:网站首页>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)
}
边栏推荐
- Creative SVG ring clock JS effect
- Hibernate learning 3 - custom SQL
- Related operations of ansible and Playbook
- Do280openshift access control -- encryption and configmap
- 信号完整性(SI)电源完整性(PI)学习笔记(一)信号完整性分析概论
- Hibernate learning 2 - lazy loading (delayed loading), dynamic SQL parameters, caching
- Report on operation pattern and future prospect of global and Chinese propyl isovalerate industry from 2022 to 2028
- 时间统一系统
- 中低速航空航天电子总线概述
- Modstart: embrace new technologies and take the lead in supporting laravel 9.0
猜你喜欢
Paper review: U2 net, u-net composed of u-net
Meta&伯克利基于池化自注意力机制提出通用多尺度视觉Transformer,在ImageNet分类准确率达88.8%!开源...
VR全景怎么赚钱?结合市场从两个方面客观分析下
How can I persuade leaders to use DDD to construct the liver project?
UE4 WebBrowser chart cannot display problems
为什么生命科学企业都在陆续上云?
Time unified system
Wx applet jump page
传输层 以字节为单位的滑动窗口技术
Tongji and Ali won the CVPR best student thesis, lifeifei won the Huang xutao award, and nearly 6000 people attended the offline conference
随机推荐
Is it so difficult to calculate the REM size of the web page according to the design draft?
Color gradient gradient color collection
Go crawler framework -colly actual combat (4) -- Zhihu answer crawl (2) -- visual word cloud
[leaderboard] Carla leaderboard leaderboard leaderboard operation and participation in hands-on teaching
无需显示屏的VNC Viewer远程连接树莓派
在滴滴和字节跳动干了 5年软件测试,太真实…
Hibernate learning 3 - custom SQL
Collective example
D does not require opapply() as a domain
Paper review: U2 net, u-net composed of u-net
Common redis commands in Linux system
软件测试与游戏测试文章合集录
Use of JMeter
After 5 years of software testing in didi and ByteDance, it's too real
What are the advantages of VR panoramic production? Why is it favored?
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
What is test development? Can you find a job at this stage?
颜色渐变梯度颜色集合
【面试题】什么是事务,什么是脏读、不可重复读、幻读,以及MySQL的几种事务隔离级别的应对方法
Ansible及playbook的相关操作