Android Navigation使用
简介
Navigation导航编辑器旨在简化Android开发中导航的实现,可以帮助我们很好的处理Activity和fragment之间通过FragmentTransaction交互的复杂性,也可以很好的处理页面的转场效果;Deeplink的支持,绕过activity直接跳到fragment;并且传递参数更安全。在Android Studio3.2可以使用。
基本使用
- 引用相关依赖
implementation "android.arch.navigation:navigation-fragment:1.0.0-rc01" // use -ktx for Kotlin
implementation "android.arch.navigation:navigation-ui:1.0.0-rc01"
- 创建资源文件
![]()
- 创建Fragment文件
class IndexFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.index_fragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
var args = arguments?.let { IndexFragmentArgs.fromBundle(it) }
top_bar_title.text = args!!.topBarTitle
txt_desc.text = "${args!!.topBarTitle}页面"
}
}
class BallFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.ball_fragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
var args = arguments?.let { BallFragmentArgs.fromBundle(it) }
top_bar_title.text = args!!.topBarTitle
txt_desc.text = "${args!!.topBarTitle}页面"
}
}
- 创建navigation导航图
<?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/indexFragment">
<!-- app:startDestination是起始Destination,必须指定 -->
<fragment android:id="@+id/indexFragment"
android:name="com.fomin.demo.bar.IndexFragment"
android:label="IndexFragment"
tools:layout="@layout/index_fragment">
<!--参数传递-->
<argument android:name="topBarTitle"
app:argType="string"
android:defaultValue="主页"/>
<!--跳转动作-->
<action android:id="@+id/action_indexFragment_to_ballFragment"
app:destination="@id/ballFragment"/>
</fragment>
<fragment android:id="@+id/ballFragment"
android:name="com.fomin.demo.bar.BallFragment"
android:label="BallFragment"
tools:layout="@layout/ball_fragment">
<argument android:name="topBarTitle"
app:argType="string"
android:defaultValue="足球"/>
</fragment>
</navigation>
- Activity布局文件添加fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/nav_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"/>
</LinearLayout>
在Activity中添加如下代码
class MainActivity2 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
}
override fun onSupportNavigateUp(): Boolean {
return Navigation.findNavController(this, R.id.nav_fragment).navigateUp()
}
}
- 配置Fragment的跳转
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
var args = arguments?.let { IndexFragmentArgs.fromBundle(it) }
top_bar_title.text = args!!.topBarTitle
btn_goto_ball.setOnClickListener { Navigation.findNavController(it).navigate(R.id.action_indexFragment_to_ballFragment) }//点击跳转时间
}
- 配置Fragment回退事件
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
var args = arguments?.let { BallFragmentArgs.fromBundle(it) }
top_bar_title.text = args!!.topBarTitle
top_bar_back.visibility = if (args!!.showBack == 1) View.VISIBLE else View.GONE
txt_desc.text = "${args!!.topBarTitle}页面"
top_bar_back.setOnClickListener { Navigation.findNavController(it).popBackStack() }//回退事件
}
好了,Navigation入门讲解完了,上面代码对于Fragment 并非是通过原生的 FragmentManager 和 FragmentTransaction 进行控制的,而是通过以下Navigation.findNavController(params)进行的控制。接下来会对Navigation详细讲解。
导航视图
- app:startDestination
此属性位于navigation 根节点上,是导航器默认加载在Activity的视图,是必须设置的。
<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/indexFragment">
<fragment android:id="@+id/ballFragment"
android:name="com.fomin.demo.bar.BallFragment"
android:label="BallFragment"
tools:layout="@layout/ball_fragment"/>
</navigation>
- fragment/activity
navigation可以添加fragment和activity的视图,需要关注的属性android:id和android:name,name是所在Fragmet/Activity类所在包名,id就不解释了,众所周知。
- argument
使用参数传递,需要module的build.gradle添加:apply plugin: 'androidx.navigation.safeargs'
<fragment android:id="@+id/ballFragment"
android:name="com.fomin.demo.bar.BallFragment"
android:label="BallFragment"
tools:layout="@layout/ball_fragment">
<argument android:name="topBarTitle"
app:argType="string"
android:defaultValue="足球"/>
</fragment>
视图之间的参数传递属性,argType可以支持string、integer、reference,long,float,boolean和Parcelable对象等。增加属性之后需要Rebuild一下,IDE会生成相关视图的Args类。如:
public class BallFragmentArgs implements NavArgs {
省略....
@NonNull
public String getTopBarTitle() {
return (String) arguments.get("topBarTitle");
}
省略....
}
参数传递
btn_goto_ball.setOnClickListener {
val bundle = Bundle()
bundle.putString("topBarTitle", "篮球")
Navigation.findNavController(it).navigate(R.id.action_indexFragment_to_ballFragment, bundle)
}
获取传递参数值
var args = arguments?.let { BallFragmentArgs.fromBundle(it) }
top_bar_title.text = args!!.topBarTitle
- action
动作,即跳转动作,从视图A跳转到视图B的动作
<fragment android:id="@+id/indexFragment"
android:name="com.fomin.demo.bar.IndexFragment"
android:label="IndexFragment"
tools:layout="@layout/index_fragment">
<!--跳转动作-->
<action android:id="@+id/action_indexFragment_to_ballFragment"
app:destination="@id/ballFragment"/>
</fragment>
app:destination的属性,声明了这个行为导航的目的地id为ballFragment的视图
android:id 这个id作为Action唯一的 标识,在视图类的某个点击事件中,我们通过id指向对应的行为
btn_goto_ball.setOnClickListener {
Navigation.findNavController(it).navigate(R.id.action_indexFragment_to_ballFragment)
}
平常页面跳转都会使用到相关的转场动画,action也为转场动画提供了enterAnim、exitAnim、popEnterAnim、popExitAnim四个动画属性,可以设置相关的anim动画资源。
此外,还提供了一个app:popUpTo属性,它的作用是声明导航行为将返回到id对应的Fragment。
- Deep Link
使用deep-link可以创建深层链接,类似activity的自定义URL使用Scheme方式来跳转,可以直接跳转到指定fragment/activity
<fragment android:id="@+id/ballFragment"
android:name="com.fomin.demo.bar.BallFragment"
android:label="BallFragment"
tools:layout="@layout/ball_fragment">
<deepLink app:uri="http://www.fomin.com/login"/>
</fragment>
在Manifest.xml添加规则
<activity android:name=".login.LoginActivity">
<nav-graph android:value="@navigation/nav_graph2"/>
</activity>
NavHostFragment
NavHostFragment在布局中提供了一个区域,用于进行Navigation。
<fragment
android:id="@+id/nav_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph2"/>
android:name指定NavHostFragment包名,必填项;app:navGraph指定navigation的资源文件;app:defaultNavHost="true"可确保NavHostFragment拦截系统“后退”按钮。 也可以在代码上设置,如:
override fun onSupportNavigateUp(): Boolean {
return Navigation.findNavController(this, R.id.nav_fragment).navigateUp()
}
导航类
navigation提供了Navigation和NavController的类;Navigation此类提供了用于从应用程序中的各个常见位置查找相关NavController实例的实用程序,或用于执行导航以响应UI事件的实用程序;而NavController管理NavHost中的应用程序导航。
Android Navigation使用的更多相关文章
- Android Navigation Drawer(导航抽屉)
Google I/O 2013 Android 更新了Support库,新版本的Support库中新加入了几个比较重要的功能. 添加 DrawerLayout 控件,支持创建 Navigation ...
- Android Navigation 架构组件入门教程
Android Navigation 架构组件入门教程 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请表明出处:https://www.cnblogs.com/cavalier-/p/1 ...
- Android Navigation Drawer,自定义ActionBar(标题居中)
整个示例都是改造自 Google Android Training 中的 NavigationDrawer 示例(http://developer.android.com/training/imple ...
- Android - Navigation Drawer
http://www.jianshu.com/p/c8cbeb7ea43a 用Navigation Drawer 和 Navigation View 来实现左右侧滑 Activity里甚至什么都不用写 ...
- 与Status Bar和Navigation Bar相关的一些东西
Android Navigation Bar Status Bar 与StatusBar和NavigationBar相关的东西有两种,一是控制它们的显示与隐藏,二是控制它们的透明与否及背景. 在2 ...
- Android基础系列合集
四大组件 1.Service Android四大组件-Service Android-远程Service Service 动态更新 UI 2.Activity Android四大组件-Activity ...
- Android 打开高德地图、百度地图进行导航;打开第三方App去导航;
抽成工具类了,复制下来就能直接用了,直接看代码吧: 高德地图Url Api: http://lbs.amap.com/api/amap-mobile/guide/android/navigation ...
- ReactNative 调用手机地图(高德、百度)导航 Android
由于项目需要,鉴于第三方sdk包的体积略大,经过评估论证后,决定采用调起APP以及网页地图的方式来进行导航. 思路: 在需要调用导航的界面通过原生获取当前手机内可用的导航app组装成列表返回到RN待选 ...
- 在Android App中集成Google登录
技术文章 来源:码农网 发布:2016-09-19 浏览:194 摘要:今天,几乎所有的web和移动app都自带谷歌和Facebook登录,这对app开发者和用户来说是一个非常有用的功能,因为几乎每个 ...
随机推荐
- 学习Axure RP原型设计
1 概述 原型设计是应用开发设计的第一要素.好的原型设计不仅可以起到沟通的作用,而且对客户而言应用程序拥有更直观的体现.原型设计通过内容和结构展示以及界面布局编排,实现在开发前期用户与产品进行交互.提 ...
- KVM虚拟化概述与安装
虚拟化是构建云计算基础架构不可或缺的关键技术之一,云计算的云端系统,其实质上就是一个大型的KVM分布式系统,虚拟化通过在一个物理平台上虚拟出更多的虚拟平台,而其中的每一个虚拟平台则可以作为独立的终端加 ...
- {黑掉这个盒子} \\ FluxCapacitor Write-Up
源标题:{Hack the Box} \ FluxCapacitor Write-Up 标签(空格分隔): CTF 好孩子们.今天我们将学习耐心和情绪管理的优点.并且也许有一些关于绕过WEB应用防 ...
- 透彻讲解,Java线程的6种状态及切换
Java中线程的状态分为6种. 1. 初始(NEW):新创建了一个线程对象,但还没有调用start()方法.2. 运行(RUNNABLE):Java线程中将就绪(ready)和运行中(running) ...
- MySQL InnoDB 行记录格式(ROW_FORMAT)
MySQL InnoDB 行记录格式(ROW_FORMAT) 一.行记录格式的分类和介绍 在早期的InnoDB版本中,由于文件格式只有一种,因此不需要为此文件格式命名.随着InnoDB引擎的发展,开发 ...
- 吴恩达机器学习笔记8-多变量线性回归(Linear Regression with Multiple Variables)--多维特征
我们探讨了单变量/特征的回归模型,现在我们对房价模型增加更多的特征,例如房间数楼层等,构成一个含有多个变量的模型,模型中的特征为(
- dubbo实用知识点总结(二)
1. 参数验证 2. 结果缓存 3. 泛化引用 客户端没有对应接口类的情况,可以直接调用 4. 泛化实现 5. 回声测试 用于检测服务是否可用 6. 上下文信息 7. 隐式传参(不常用) 8. 异步调 ...
- The SDK 'Microsoft.NET.Sdk' specified could not be found.
有一台电脑用 VS Code 开发 .NET Core 项目时,每次打开文件夹都有一个错误(标题),定位在 C# 插件,鼠标放在代码上没有智能提醒,输入代码时没有补全提示,重装 VS Code 和所有 ...
- Collection 和 Collections;Array与Arrays的区别
Collection 和 Collections 的区别. Collection 是个 java.util 下的接口 ,它是各种集合结构的父接口.继承与他的接口主要有 Set 和 List. Col ...
- Kubernetes系列之Coredns and Dashboard介绍篇
本次系列使用的所需部署包版本都使用的目前最新的或最新稳定版,安装包地址请到公众号内回复[K8s实战]获取 介绍 项目地址:https://github.com/coredns/coredns Core ...