原文链接
1). 依赖
dependencies {
def nav_version = "1.0.0-alpha07"
implementation "android.arch.navigation:navigation-fragment:$nav_version" // use -ktx for Kotlin
implementation "android.arch.navigation:navigation-ui:$nav_version" // use -ktx for Kotlin
// optional - Test helpers
// this library depends on the Kotlin standard library
androidTestImplementation "android.arch.navigation:navigation-testing:$nav_version"
}
- Safe args
buildscript {
// google仓库
maven {
url 'https://maven.aliyun.com/repository/google'
}
dependencies {
classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha07"
}
}
在模块的build.gradle文件中添加
apply plugin: "androidx.navigation.safeargs"
2). 开启编辑器
File -> Settings -> Experimental -> Editor -> 勾选Enable Navigatioon Editor.
图1.jpg
3). 创建导航图
I. 创建资源文件
在res文件夹上右键,选择Android Resource File
图2.png
II. 创建nav_graph.xml文件
输入文件名(File Name) 为nav_graph,并选择资源类型(Resource type) 为Navigation,此处如果没有勾选上一步的编辑器,则资源类型中没有Navigation选项.
图3.png
III. 目录
点击OK之后,res目录下会出现navigation文件夹,navigation文件夹下会出现nav_graph.xml文件
图4.png
IV. nav_graph.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"
android:id="@+id/nav_graph">
</navigation>
V. 编辑器样式
图5.png
4). 创建Fragment
I. 点击图5中的红色部位按钮(New Destination)创建Fragment
图6.png
II. 选择Create blank destination
图7.png
III. 连续创建两个,重复上面操作即可
IV. 连接
图8.png
V. 设置后的nav_graph.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_graph"
app:startDestination="@id/blankFragment">
<fragment
android:id="@+id/blankFragment"
android:name="com.mazaiting.jetpack.BlankFragment"
android:label="fragment_blank"
tools:layout="@layout/fragment_blank">
<action
android:id="@+id/action_blankFragment_to_blankFragment2"
app:destination="@id/blankFragment2"/>
</fragment>
<fragment
android:id="@+id/blankFragment2"
android:name="com.mazaiting.jetpack.BlankFragment2"
android:label="fragment_blank_fragment2"
tools:layout="@layout/fragment_blank_fragment2"/>
</navigation>
VI. 修改Fragment位置
此时Framgent位置在包名下,在指定位置下创建fragment文件夹,将新生成的fragment文件移动即可。
VII. 新nav_graph.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_graph"
app:startDestination="@id/blankFragment">
<fragment
android:id="@+id/blankFragment"
android:name="com.mazaiting.jetpack.architecture.navigation.fragment.BlankFragment"
android:label="fragment_blank"
tools:layout="@layout/fragment_blank">
<action
android:id="@+id/action_blankFragment_to_blankFragment2"
app:destination="@id/blankFragment2"/>
</fragment>
<fragment
android:id="@+id/blankFragment2"
android:name="com.mazaiting.jetpack.architecture.navigation.fragment.BlankFragment2"
android:label="fragment_blank_fragment2"
tools:layout="@layout/fragment_blank_fragment2"/>
</navigation>
5). 在Activity中添加Fragment
在activity_navigation.xml文件中添加fragment
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.mazaiting.jetpack.architecture.navigation.NavigationActivity">
<fragment
android:name="androidx.navigation.fragment.NavHostFragment"
android:id="@+id/nav_host_fragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</android.support.constraint.ConstraintLayout>
II. 在Activity中重写onNavigateUp()方法
override fun onNavigateUp(): Boolean = findNavController(this, R.id.nav_host_fragment).navigateUp()
III. 初始化
/**
* 如果布局文件的fragment中没有这两行,则需要设置如下内容
* app:defaultNavHost="true"
* app:navGraph="@navigation/nav_graph"
*/
val finalHost = NavHostFragment.create(R.navigation.nav_graph)
supportFragmentManager.beginTransaction()
.replace(R.id.nav_host_fragment, finalHost)
.setPrimaryNavigationFragment(finalHost)
.commit()
6). 添加按钮
在blankFragment和blankFragment2中分别添加按钮
7). 点击跳转
I. BlankFragment中
val btnShow: Button = view.findViewById(R.id.btn_show_fragment)
btnShow.setOnClickListener { btnView ->
// 跳转下一个页面
Navigation.findNavController(btnView).navigate(R.id.action_blankFragment_to_blankFragment2)
}
II. BlankFragment2中
val btnShow: Button = view.findViewById(R.id.btn_show_fragment2)
btnShow.setOnClickListener { btnView ->
// 返回上一页
Navigation.findNavController(btnView).navigateUp()
}
III. 效果图
test.gif
网友评论