Android—Navigation的使用
Navigation是Fragment的一个容器,用于管理Fragment。
我们可以通过它实现Fragment的跳转以及传值等操作。
1、先写Fragment的xml文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"xmlns:app="http://schemas.android.com/apk/res-auto"><TextViewandroid:id="@+id/tv"android:gravity="center"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintLeft_toLeftOf="parent"android:layout_width="200dp"android:layout_height="60dp"android:text="跳转到第二个Fragment"android:background="@color/white"/></androidx.constraintlayout.widget.ConstraintLayout>
2、写Fragment的java文件
public class FirstFragment extends Fragment {@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {return inflater.inflate(R.layout.first_fragment,container,false);}@Overridepublic void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {super.onViewCreated(view, savedInstanceState);TextView b = view.findViewById(R.id.tv);b.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Navigation.findNavController(getActivity(),R.id.fragment).navigate(R.id.secondFragment);}});}
}
findNavController()方法就是获取NavController,id指向的是navigation,navigate方法就是跳转到指定id的fragment。
第二个Fragment也同上两个步骤
3、先定义一个fragment容器,Navgtion的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_fragment"app:startDestination="@id/firstFragment"><fragmentandroid:id="@+id/firstFragment"android:name="com.example.myapplication.FirstFragment"><actionapp:enterAnim="@anim/fragment_fast_out_extra_slow_in"app:exitAnim="@anim/nav_default_pop_exit_anim"android:id="@+id/toSecondFragment"app:destination="@+id/secondFragment"/></fragment><fragmentandroid:id="@+id/secondFragment"android:name="com.example.myapplication.SecondFragment"><actionapp:enterAnim="@anim/fragment_fade_enter"app:popExitAnim="@anim/fragment_close_exit"android:id="@+id/toFirstFragment"app:destination="@+id/firstFragment"/></fragment></navigation>
将两个fragment都添加进容器,为其添加action,可指定进出动画等。
4、在activity布局中添加navigation,之前是把所有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:name="androidx.navigation.fragment.NavHostFragment"app:navGraph = "@navigation/nav_fragment"android:id="@+id/fragment"android:layout_height="500dp"android:layout_width="500dp"app:layout_constraintTop_toTopOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintRight_toRightOf="parent"/></androidx.constraintlayout.widget.ConstraintLayout>
name字段是固定的。
通过navGraph字段fragment标签与navigation的xml文件绑定。
直接运行就可以实现点击Fragment中的textView进行跳转。
关于BootomNavigation的使用
需要在activity中让底部导航栏与navigation绑定
public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);BottomNavigationView navView = findViewById(R.id.nav_view);AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications1, R.id.a, R.id.b).build();NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);NavigationUI.setupWithNavController(navView, navController);}}
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder( R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications1, R.id.a, R.id.b) .build();
Builder参数的id需要与navigation中fragment的id和menu中的id保持一致。前后顺序不影响。fragment的xml和java文件和menu文件就不展示了。
setupWithNavController()方法中就是通过比较fragment的id与menu的id进行联动。
static boolean matchDestination(@NonNull NavDestination destination,@IdRes int destId) {NavDestination currentDestination = destination;while (currentDestination.getId() != destId && currentDestination.getParent() != null) {currentDestination = currentDestination.getParent();}return currentDestination.getId() == destId;}
acitvity的布局:
<?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"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingTop="?attr/actionBarSize"><com.google.android.material.bottomnavigation.BottomNavigationViewandroid:id="@+id/nav_view"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginStart="0dp"android:layout_marginEnd="0dp"app:itemIconTint="@color/teal_200"app:itemTextColor="@color/teal_700"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:menu="@menu/bottom_nav_menu" /><fragmentandroid:id="@+id/nav_host_fragment"android:name="androidx.navigation.fragment.NavHostFragment"android:layout_width="match_parent"android:layout_height="match_parent"app:defaultNavHost="true"app:layout_constraintBottom_toTopOf="@id/nav_view"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:navGraph="@navigation/mobile_navigation" /></androidx.constraintlayout.widget.ConstraintLayout>
itemIconTint和itemTextColor可以改变底部图标颜色和字体颜色。
Navigation的缺点是跳转方法
navigate(int)会不断创建新的fragment,不支持复用。
解决方法:https://www.jianshu.com/p/adae9494d822
Android—Navigation的使用相关推荐
- Android Navigation Drawer(导航抽屉)
Google I/O 2013 Android 更新了Support库,新版本的Support库中新加入了几个比较重要的功能. 添加 DrawerLayout 控件,支持创建 Navigation ...
- 有关Android导览(Android Navigation component)
文章目录 小结 有关Android导览(Android Navigation component) 碰到的问题 参考 小结 在使用Android导览(Android Navigation compon ...
- android navigation bar高度,Android获取屏幕真实高度包含NavigationBar(底部虚拟按键)
释放双眼,带上耳机,听听看~! public int getScreentHeight() { int heightPixels; WindowManager w = this.getWindowMa ...
- Android Navigation 组件(基础篇)
一.前言 在日常开发中,越来越多的会使用到一个activity嵌套多个fragment的场景,典型的例子就是app的首页,一般都会由一个activity+多个Fragment组成的底部导航界面,那对于 ...
- Android Navigation与BottomNavigationView实现底部导航栏
底部导航栏 一.效果图 二.实现 1.创建Fragment以及布局文件 2.添加FragmentContainerView和BottomNavigationView两个控件 3.配置xml资源文件 4 ...
- Android | navigation入门详解
本文来自博客园,作者:{观心静},转载请注明原文链接: {https://www.cnblogs.com/guanxinjing/} 本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此 ...
- Android - Navigation
文章目录 1. 创建Fragment 2. 添加Navigation 3. activity_main中添加NavHostFragment容器 4. MainActivity.java 5. 两个Fr ...
- Android Navigation使用
简介 Navigation导航编辑器旨在简化Android开发中导航的实现,可以帮助我们很好的处理Activity和fragment之间通过FragmentTransaction交互的复杂性,也可以很 ...
- Android - Navigation组件
Navigation 组件使用入门 设置你的环境 注意:Navigation 组件需要 Android Studio 3.3 或更高版本,并且依赖于 Java 8 语言功能. 如需在您的项目中添加 N ...
最新文章
- C指针原理(43)-helloworld的C程序汇编剖析
- jsp java语法_JSP基础语法
- php 操作 mysql 数据库常用方法集合
- Docker 从Dockerfile 构建镜像 :build 命令的用法
- Python中groupby的简单使用
- 《Android深入透析》之界面
- ffmpeg拉流设置暂停_解决ffmpeg拉流转发频繁丢包问题max delay reached. need to consume packet...
- 【重构与模式】6.1用Creation Method替换构造函数
- 深圳市区卫星地图 百度卫星地图高清版(含道路地名标签叠加)
- 2023年长安大学外国语言文学考研考情与难度及上岸前辈备考经验
- OCR手写字体生成数据
- ffmpeg实现视频切割
- 论文阅读《Context-Transformer:Tackling Object Confusion for Few-Shot Detection》
- 西南大学计算机辅助设计试题,西南大学 1906 课程名称:(9123)《计算机辅助设计》机考 答案-奥...
- Git 奇淫技巧:Github ID 更名奇遇记
- 【机器学习】Python详细实现基于欧式Euclidean、切比雪夫Chebyshew、曼哈顿Manhattan距离的Kmeans聚类
- Halcon显示正常比列长宽比的方法
- 温暖的奢侈,当手机遇到机器人
- 阿里巴巴java工程师应聘条件_【全国】—前端/Java工程师—阿里巴巴(长期招聘)...
- 2019年我的技术自我救赎之路
热门文章
- mysql scott用户_在mysql中创建 oracle scott 用户的四个表及插入初始化数据
- 周期三角波傅里叶级数例题_傅里叶详解之傅里叶级数
- 织梦cms提交熊掌号php,织梦cms熊掌号历史数据提交教程
- mysql+多列外键_MySQL中的多列外键?
- pythonturtle库使用心得_记录我的Python学习之旅(一)关于turtle库的基本用法
- gerber文件怎么导贴片坐标_PCBA贴片加工厂家的上机贴片编程
- python dict函数key_python的dict判断key是否存在的方法
- 批量梯度下降python实现_python实现梯度下降算法的实例详解
- 高压电是以交流,还是直流方式输送?
- 在中国做操作系统研发 20 年是种什么体验?