Navigation的简单使用
文章目录
- Navigation导航
- 说明
- 基本使用
- 第一步:导入引用
- 第二步:创建需要导航的`Fragment`
- 第三步: 编写导航指引图表
- 第四步:将需要导航的`fragment`配置进入导航指引图表
- 第五步:配置默认的第一个页面
- 第六步:设置每个页面间的跳转关系
- 第七步:在页面中引用导航指引图表
- 第八步:编写每个导航页中的跳转事件按钮
- 第九步:代码中触发导航跳转事件
- 项目地址
Navigation导航
说明
详情情况谷歌开发者官网
google的API
google在github的代码示例
基本使用
第一步:导入引用
def navigationVersion = '2.3.5'implementation "android.arch.navigation:navigation-fragment-ktx:$navigationVersion"implementation "android.arch.navigation:navigation-ui-ktx:$navigationVersion"
需要引入两个包,查看最新版本的话可以使用下方链接去查看
navigation-fragment-ktx
navigation-ui-ktx
第二步:创建需要导航的Fragment
这一步没什么好说的了
第三步: 编写导航指引图表
在资源目录
res
目录下新建navigation
目录在
navigation
目录下新建导航指引图表nav_graph_main.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_main"></navigation>
第四步:将需要导航的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_main"><fragmentandroid:id="@+id/fragment_1"android:name="com.fengshan.navigationdemo.Fragment1"android:label="fragment1"></fragment><fragmentandroid:id="@+id/fragment_2"android:name="com.fengshan.navigationdemo.Fragment2"android:label="fragment2"></fragment><fragmentandroid:id="@+id/fragment_3"android:name="com.fengshan.navigationdemo.Fragment3"android:label="fragment3"></fragment></navigation>
第五步:配置默认的第一个页面
关键代码:
app:startDestination="@id/fragment_1"
全局代码:
<?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_main"app:startDestination="@id/fragment_1"><fragmentandroid:id="@+id/fragment_1"android:name="com.fengshan.navigationdemo.Fragment1"android:label="fragment1"></fragment><fragmentandroid:id="@+id/fragment_2"android:name="com.fengshan.navigationdemo.Fragment2"android:label="fragment2"></fragment><fragmentandroid:id="@+id/fragment_3"android:name="com.fengshan.navigationdemo.Fragment3"android:label="fragment3"></fragment></navigation>
第六步:设置每个页面间的跳转关系
在nav_graph_main.xml
文件中加入下方关键性代码块
关键性代码:
<fragmentandroid:id="@+id/fragment_1"android:name="com.fengshan.navigationdemo.Fragment1"android:label="fragment1"><actionandroid:id="@+id/action_1_to_2"app:destination="@id/fragment_2"></action></fragment>
destination:目标页面
完整代码:
<?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_main"app:startDestination="@id/fragment_1"><fragmentandroid:id="@+id/fragment_1"android:name="com.fengshan.navigationdemo.Fragment1"android:label="fragment1"tools:layout="@layout/fragment_1"><actionandroid:id="@+id/action_1_to_2"app:destination="@id/fragment_2"></action><actionandroid:id="@+id/action_1_to_3"app:destination="@id/fragment_3"></action></fragment><fragmentandroid:id="@+id/fragment_2"android:name="com.fengshan.navigationdemo.Fragment2"android:label="fragment2"tools:layout="@layout/fragment_2"><actionandroid:id="@+id/action_2_to_3"app:destination="@id/fragment_3"></action><actionandroid:id="@+id/action_2_to_1"app:destination="@id/fragment_1"></action></fragment><fragmentandroid:id="@+id/fragment_3"android:name="com.fengshan.navigationdemo.Fragment3"android:label="fragment3"tools:layout="@layout/fragment_3"><actionandroid:id="@+id/action_3_to_1"app:destination="@id/fragment_1"></action><actionandroid:id="@+id/action_3_to_2"app:destination="@id/fragment_2"></action></fragment></navigation>
第七步:在页面中引用导航指引图表
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><fragmentandroid:id="@+id/main_fragment"android:name="androidx.navigation.fragment.NavHostFragment"android:layout_width="match_parent"android:layout_height="match_parent"app:defaultNavHost="true"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:navGraph="@navigation/nav_graph_main"></fragment>
</androidx.constraintlayout.widget.ConstraintLayout>
defaultNavHost
:是否拦截返回键事件
android:name="androidx.navigation.fragment.NavHostFragment"
固定写法
navGraph
指定此fragment使用的导航指引图表
第八步:编写每个导航页中的跳转事件按钮
在每个导航页中编写跳转页面的事件按钮是因为当前只是单独使用的navigation来开发,只能使用按钮来触发跳转事件了
下方是其中一个的导航页布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"tools:context=".Fragment1"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="我是fragment1" /><Buttonandroid:id="@+id/fragment_1_btn_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="跳转至 页面2" /><Buttonandroid:id="@+id/fragment_1_btn_3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="跳转至 页面3" /></LinearLayout></FrameLayout>
第九步:代码中触发导航跳转事件
在导航页中为对应的按钮添加点击事件并触发导航跳转事件
关键性代码块:
val view = inflater.inflate(R.layout.fragment_1, container, false)val btn1 = view.findViewById<Button>(R.id.fragment_1_btn_2)btn1.setOnClickListener {Navigation.findNavController(it).navigate(R.id.action_1_to_2)}
项目地址
最近github的速度堪忧,梯子太贵.以后主要在gitee上面玩耍吧,省着那天又上不去了
gitee传送门
Navigation的简单使用相关推荐
- Android:安卓学习笔记之navigation的简单理解和使用
Android navigation的简单理解和使用 1 .基本概念 1.1.背景 1.2.含义 2.组成 2.1.Navigation graph 2.2.NavHostFragment 2.3.N ...
- Fiori navigation logic ( Route )
对于Fiori的developer来说,要在代码里做view之间的navigation很简单,就做一些配置,然后call 一行代码,思路非常像webclient ui里的inbound和outboun ...
- android导航使用教程,android BottomNavigationView的简单使用教程
每个android app都有BottomNavigationView导航,本人开发中刚刚使用到了BottomNavigationView,于是按照android developer官网特意做了一个符 ...
- css设置打印样式表,[CSS] 创建打印样式表
[CSS] 创建打印样式表 Chrome浏览器有一项非常好用的功能, 就是直接将网页保存为PDF(Ctrl+P), 虽然部分人觉得没有用, 但我经常使用这个功能 比如说, 有的博文写得不错, 想保存, ...
- ArcGIS 构建3D动画方法
Ⅰ 以动画表现视图(View) 1 捕获透视图(Capturing perspective views) 使用"动画"工具条中的"捕获视图"工具 捕获视图 可以 ...
- iOS开发 : Navigation Bar的简单设置
前面的一篇文章<iOS开发16:使用Navigation Controller切换视图>中的小例子在运行时,屏幕上方出现的工具栏就是Navigation Bar,而所谓UINavigati ...
- 一个简单的storyboard示例,其中关于添加navigation的部分可以学习,此前没用过
到storyboard选中我们唯一一个的viewcontroller,找到xcode的菜单栏,Edit->Embed In->NavigationController.这时候storybo ...
- tcp reno_如何使用称为Reno Expo的简单入门工具包构建全栈应用程序
tcp reno Building any new project from scratch can be intimidating. There's a lot to decide before y ...
- 英语面试简短问题_用简单的英语解释产品设计
英语面试简短问题 Product design is the process you go through when you conceptualize and build a product. 产品 ...
最新文章
- Mybatis插入MySQL数据库中文乱码
- 字符串缓冲区太小怎么解决_epoll的两种模式 ET和LT printf的缓冲区问题 边缘非阻塞模式...
- 计算机科学速成视频35,计算机科学速成课30:万维网【视频】
- STL 之includes,set_intersection,set_union,set_difference,set_symmetric_difference
- c语言中英文的作用,C语言中英文对照.doc
- 协方差意味着什么_“零”到底意味着什么?
- multi source replication mysql_MySQL 5.7多源复制(Multi-Source Replication)
- 华为内测基于Android 10.0的EMUI 10系统;2019年Q1真无线耳机市场份额,苹果占半壁江山……...
- Windows 10如何消除文件夹右上角的“相对箭头”?
- ZooKeeper官方文档学习笔记01-zookeeper概述
- Red Hat TimesTen安装记录
- Linux Centos 常用命令整理
- 360电脑网速怎么测试软件,win7使用360安全卫士测试网速的方法 win7攻略
- crontab 问号_轻松搞定crontab和quartz表达式
- java pdf替换文字_java代码用itext 识别PDF中的文字然后替换
- Android ImageView属性
- 【前端】使用html+css+js实现的乞丐版跳一跳
- C语言基础 龟兔赛跑
- 图解复盘总结和报告模板实例
- pip install pygame_Python、PyGame游戏项目
热门文章
- 科学,宗教和信仰,以及神
- python爬虫——使用selenium爬取微博数据(一)
- HTC VIVE 禁用头盔定位与角度旋转
- 使用RedisTemplate 设置key有前缀 \xAC\xED\x00\x05t\x00\x04 value有前缀 \xAC\xED\x00\x05t\x00\x08 问题解决
- java实现StringBuffer小案例
- python asyncio future_Python 期物之 asyncio.Future
- 振动开关和水银开关的区别
- Fragment销毁自己
- MQTT学习笔记之Mosquitto的安装和使用
- 微型 Python Web 框架 Bottle - Heroin blog