Navigation(导航)
简介
导航是指支持用户导航、进入和退出应用中不同内容片段的交互。Android Jetpack 的导航组件可帮助您实现导航,无论是简单的按钮点击,还是应用栏和抽屉式导航栏等更为复杂的模式,该组件均可应对。导航组件还通过遵循一套既定原则来确保一致且可预测的用户体验。
组成
导航组件由以下三个关键部分组成:
- 导航图:在一个集中位置包含所有导航相关信息的 XML 资源。这包括应用内所有单个内容区域(称为目标)以及用户可以通过应用获取的可能路径。
- NavHost:显示导航图中目标的空白容器。导航组件包含一个默认 NavHost 实现 (NavHostFragment),可显示 Fragment 目标。
- NavController:在 NavHost 中管理应用导航的对象。当用户在整个应用中移动时,NavController 会安排 NavHost 中目标内容的交换。
在应用中导航时,您告诉 NavController,您想沿导航图中的特定路径导航至特定目标,或直接导航至特定目标。NavController 便会在 NavHost 中显示相应目标。
优势
- 处理 Fragment 事务。
- 默认情况下,正确处理往返操作。
- 为动画和转换提供标准化资源。
- 实现和处理深层链接。
- 包括导航界面模式(例如抽屉式导航栏和底部导航),用户只需完成极少的额外工作。
- Safe Args - 可在目标之间导航和传递数据时提供类型安全的 Gradle 插件。
- ViewModel 支持 - 您可以将 ViewModel 的范围限定为导航图,以在图表的目标之间共享与界面相关的数据。
此外,您还可以使用 Android Studio 的 Navigation Editor 来查看和编辑导航图。
基本使用
1.添加依赖:
dependencies {
def nav_version = "2.3.5"
// Java language implementation
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"
// Kotlin
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
// Feature module Support
implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
// Testing Navigation
androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"
// Jetpack Compose Integration
implementation "androidx.navigation:navigation-compose:1.0.0-alpha10"
}
2.创建多个Fragment(LoginFragment,RegisterFragment,ForgeFragment)
3.MainActivity添加xml布局
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
id,name必须要
navGraph:@navigation/nav_graph 创建文件navigation
defaultNavHost:返回键交给fragment处理
<?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"
android:id="@+id/nav_graph">
</navigation>
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
app:startDestination="@id/blankFragment">
<fragment
android:id="@+id/blankFragment"
android:name="com.example.cashdog.cashdog.BlankFragment"
android:label="Blank"
tools:layout="@layout/fragment_blank" />
</navigation>
在这个navigation里添加fragment (id,name必须要)
默认显示app:startDestination=“id名”
navigation跳转
在fragmet中 添加action属性 id ,name
<fragment
android:id="@+id/blankFragment"
android:name="com.example.cashdog.cashdog.BlankFragment"
android:label="fragment_blank"
tools:layout="@layout/fragment_blank" >
<action
android:id="@+id/action_blankFragment_to_blankFragment2"
app:destination="@id/blankFragment2" />
<action
android:id="@+id/action_blankFragment_to_activity"
app:destination="@id/activity" />
</fragment>
<fragment
android:id="@+id/blankFragment2"
android:name="com.example.cashdog.cashdog.BlankFragment2"
android:label="fragment_blank_fragment2"
tools:layout="@layout/fragment_blank_fragment2" />
<activity
android:id="@+id/activity"
android:name="com.example.cashdog.cashdog.BlankFragment2"/>
跳转Fragment
findNavController.navigate(R.id.action_blankFragment_to_blankFragment2)
跳转Activity
加 id,name
加 id , destination
切换Fragment的动画
创建动画(anim):
slide_from_right_to_left_in
xml: 中
translate fromXdelta=“100%p” toXdelta=“0” duration=“400”
slide_from_right_to_left_out
xml: 中
translate fromXdelta=“0” toXdelta="-100%p" duration=“400”
在<action 中添加 enterAnim进入动画 exitAnim退出动画
popEnterAnim popexitAnim(另一个Fragment)动画效果
全局Activity配置动画切换
在resource资源配置四种动画效果。
<style name="" parent="动画"> 四种item</>
navOption中
findNavController.navigate(id ,args ,navOptions,)
跳转与回退
回退两种方式
findNavController.navigateUp() 左上角返回的箭头时调用
findNavController.popBackStack() 按返回键时调用(出栈)
Navigation-popUpTo,popToinclusive
多维跳转,回到第一个界面。点击注册—》 注册完 --》设置头像—》回到登录界面
<?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_graph"
app:startDestination="@id/a">
<fragment android:id="@+id/a"
android:name="com.example.myapplication.FragmentA"
android:label="a"
tools:layout="@layout/a">
<action android:id="@+id/action_a_to_b"
app:destination="@id/b"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim"/>
</fragment>
<fragment android:id="@+id/b"
android:name="com.example.myapplication.FragmentB"
android:label="b"
tools:layout="@layout/b">
<action android:id="@+id/action_b_to_a"
app:destination="@id/a"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim"
app:popUpTo="@+id/a"
app:popUpToInclusive="true"/>
</fragment>
</navigation>
<fragment
android:id="@+id/c"
android:name="com.example.myapplication.C"
android:label="fragment_c"
tools:layout="@layout/fragment_c">
<action
android:id="@+id/action_c_to_a"
app:destination="@id/a"
app:popUpTo="@+id/a"
app:popUpToInclusive="true"/>
</fragment>
findNavController().navigate(R.id.action_b_to_a)
元素共享
在两个不同页面加入ImageView 相同transitionName
xml中
ImageView 加入 android:transitionName=“–”
在fragment A中 设置transitionName
val extras = FragmentNavigatorExtras(view1 to "hero_image")
//imageview和transitionName
view.findNavController().navigate(
R.id.confirmationAction,
null, // Bundle of args
null, // NavOptions
extras)
在fragmentB中 在 onCreate
sharedElementEnterTransition = TransitionInflater.from(requireContext())
.inflateTransition(R.transition.shared_image)
//R.transition.shared_image 创建transition文件
<!-- res/transition/shared_image.xml -->
<transitionSet>
<changeImageTransform android:duration = "400"/>
</transitionSet>
changeImageTransform继承Transition类 ctrl +H看相关类 AutoTransition
多个元素共享
val imagePair =Pair<View,String>(view1,"her_image")
val imagePair2 =Pair<View,String>(view1,"her_image")
val extras = FragmentNavigatorExtras(imagePair,imagePair2)
//imageview和transitionName
view.findNavController().navigate(
R.id.confirmationAction,
null, // Bundle of args
null, // NavOptions
extras)
到Activity目的地元素共享
1.设置共享元素 transitionName。(两个view)
2.设置navigation跳转
// Rename the Pair class from the Android framework to avoid a name clash
import android.util.Pair as UtilPair
...
val options = ActivityOptionsCompat.makeSceneTransitionAnimation(requireActivity(),
UtilPair.create(view1, "hero_image"))
val extras = ActivityNavigatorExtras(options)
view.findNavController().navigate(
R.id.details,
null, // Bundle of args
null, // NavOptions
extras)
跳转Fragment传参
在Bundle传入参数
fragment获取通过
arguments?.getString(“key”) 获取Bundle中的值
跳转Activity传参
在Bundle传入参数
fragment获取通过
intent.getStringExtra(“key”) 获取Bundle中的值
BottomNavigationView与Navigation使用
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
bottomNavigationView.setupWithNavController(navController)