设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间07 现在时间07 缓存数据 没有什么事情是简单轻松的,唯有不断努力,才能更加顺利。

没有什么事情是简单轻松的,唯有不断努力,才能更加顺利。

查看: 1514|回复: 2

Android中的导航navigation的使用详细步骤

[复制链接]

  离线 

TA的专栏

  • 打卡等级:无名新人
  • 打卡总天数:1
  • 打卡月天数:0
  • 打卡总奖励:18
  • 最近打卡:2024-07-22 19:10:15
等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
29
主题
25
精华
0
金钱
112
积分
60
注册时间
2023-9-29
最后登录
2025-3-11

发表于 2024-9-11 15:54:58 | 显示全部楼层 |阅读模式
目录


  • 使用例子
  • 使用例子2
Android中的导航(Navigation)是一种应用程序设计模式,它通过使用统一的用户界面来管理应用程序中的各种界面和交互。在Android中,导航主要通过使用Navigation SDK来实现,该SDK提供了一组工具和组件,可以帮助开发人员构建具有一致性和可访问性的用户界面。
下面是使用Android导航的详细步骤:
1.添加依赖项:首先,确保在项目的build.gradle文件中添加了Android Navigation的依赖项。可以使用Gradle或Maven进行管理。
  1. dependencies {
  2.     implementation 'androidx.navigation:navigation-fragment-ktx:2.x.x'
  3.     implementation 'androidx.navigation:navigation-ui-ktx:2.x.x'
  4. }
复制代码
2.创建Navigation Graph:导航图(Navigation Graph)是一个XML文件,用于描述应用程序中的各种界面和交互。它定义了从一个界面到另一个界面的导航路径。可以使用Android Studio中的导航编辑器来创建和编辑导航图。
3.使用导航组件:导航SDK提供了多种导航组件,例如Navigation Drawer、Navigation Menu、Fragment Transitions等,可以根据需要选择合适的组件。可以使用布局编辑器将导航组件添加到布局文件中,并使用相应的代码进行配置。
4.配置界面:在导航图中定义了各种界面,包括Activity、Fragment等。需要将这些界面添加到导航图中,并指定它们之间的导航关系。
5.启动导航:可以使用Navigation类来启动导航。可以通过调用findNavController()方法获取对导航控制器(Navigation Controller)的引用,并使用该控制器来执行导航操作。
6.实现自定义导航动作:如果需要实现自定义导航动作,可以在导航图中定义自定义动作,并使用相应的代码实现。可以使用Navigation类提供的API来执行自定义动作,例如navigate()方法。
7.调试和测试:使用Android Studio中的调试工具和模拟器来测试应用程序中的导航功能,确保导航图和代码正确无误。


使用例子

如下是一个在Android中使用Kotlin进行导航的一个简单例子,涉及创建一个简单的应用程序,其中包含一个底部的导航栏,用户可以通过它从一个屏幕导航到另一个屏幕。以下是实现这一功能的基本步骤:
1.添加依赖项:确保在
  1. build.gradle
复制代码
(Module: app)文件中添加了Android Navigation组件的依赖项。
  1. dependencies {
  2.     implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
  3.     implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
  4.     implementation 'androidx.lifecycle:lifecycle-fragment:2.4.1'
  5. }
复制代码
2.创建BottomNavigationView:在布局文件中添加
  1. BottomNavigationView
复制代码
  1. <!-- res/layout/activity_main.xml -->
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context=".MainActivity">
  8.     <androidx.bottomnavigation.widget.BottomNavigationView
  9.         android:id="@+id/nav_view"
  10.         android:layout_width="0dp"
  11.         android:layout_height="wrap_content"
  12.         app:layout_constraintBottom_toBottomOf="parent"
  13.         app:layout_constraintLeft_toLeftOf="parent"
  14.         app:layout_constraintRight_toRightOf="parent"
  15.         app:itemBackground="@color/colorPrimary"
  16.         app:itemIconTint="@android:color/white"
  17.         app:itemTextColor="@android:color/white" />
  18. </androidx.constraintlayout.widget.ConstraintLayout>
复制代码
3.配置Navigation Graph:创建一个导航图,定义几个目标(Destinations),例如
  1. FirstFragment
复制代码
,
  1. SecondFragment
复制代码
, 和
  1. ThirdFragment
复制代码
  1. <!-- res/navigation/nav_graph.xml -->
  2. <navigation xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:id="@+id/nav_graph"
  6.     app:startDestination="@+id/firstFragment">
  7.     <fragment
  8.         android:id="@+id/firstFragment"
  9.         android:name=".FirstFragment"
  10.         android:label="First Fragment"
  11.         tools:layout="@layout/fragment_first">
  12.         <!-- 添加子导航 -->
  13.         <action
  14.             android:id="@+id/action_firstFragment_to_secondFragment"
  15.             app:destination="@+id/secondFragment" />
  16.     </fragment>
  17.     <fragment
  18.         android:id="@+id/secondFragment"
  19.         android:name=".SecondFragment"
  20.         android:label="Second Fragment"
  21.         tools:layout="@layout/fragment_second">
  22.         <action
  23.             android:id="@+id/action_secondFragment_to_thirdFragment"
  24.             app:destination="@+id/thirdFragment" />
  25.     </fragment>
  26.     <fragment
  27.         android:id="@+id/thirdFragment"
  28.         android:name=".ThirdFragment"
  29.         android:label="Third Fragment"
  30.         tools:layout="@layout/fragment_third"/>
  31. </navigation>
复制代码
4.在Activity中设置NavController:在
  1. MainActivity
复制代码
中设置
  1. NavController
复制代码
并绑定到
  1. BottomNavigationView
复制代码
  1. // res/layout/activity_main.xml
  2. // 引入NavController
  3. import androidx.navigation.NavController
  4. import androidx.navigation.NavDestination
  5. import androidx.navigation.NavGraph
  6. import androidx.navigation.Navigation
  7. class MainActivity : AppCompatActivity() {
  8.     private lateinit var navController: NavController
  9.     override fun onCreate(savedInstanceState: Bundle?) {
  10.         super.onCreate(savedInstanceState)
  11.         setContentView(R.layout.activity_main)
  12.         // 获取BottomNavigationView的NavController
  13.         navController = Navigation.findNavController(this, R.id.nav_view)
  14.         // 设置NavController的监听器
  15.         navController.addOnDestinationChangedListener { _, destination, _ ->
  16.             when (destination.id) {
  17.                 R.id.firstFragment -> {
  18.                     // 执行第一个fragment的逻辑
  19.                 }
  20.                 R.id.secondFragment -> {
  21.                     // 执行第二个fragment的逻辑
  22.                 }
  23.                 R.id.thirdFragment -> {
  24.                     // 执行第三个fragment的逻辑
  25.                 }
  26.             }
  27.         }
  28.     }
  29. }
复制代码
5.创建Fragments:创建三个Fragment,每个Fragment都有自己的布局和功能。
  1. // res/layout/fragment_first.xml
  2. <!-- 第一个Fragment的布局 -->
  3. class FirstFragment : Fragment() {
  4.     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  5.         return inflater.inflate(R.layout.fragment_first, container, false)
  6.     }
  7.     // 第一个Fragment的方法和逻辑
  8. }
  9. // res/layout/fragment_second.xml
  10. <!-- 第二个Fragment的布局 -->
  11. class SecondFragment : Fragment() {
  12.     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  13.         return inflater.inflate(R.layout.fragment_second, container, false)
  14.     }
  15.     // 第二个Fragment的方法和逻辑
  16. }
  17. // res/layout/fragment_third.xml
  18. <!-- 第三个Fragment的布局 -->
  19. class ThirdFragment : Fragment() {
  20.     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  21.         return inflater.inflate(R.layout.fragment_third, container, false)
  22.     }
  23.     // 第三个Fragment的方法和逻辑
  24. }
复制代码
这个例子展示了一个基本的导航流程,用户可以在三个Fragment之间切换。每个Fragment都有其自己的布局和逻辑。通过底部的导航栏,用户可以导航到不同的Fragment。

使用例子2

使用Android Navigation库实现Fragment之间的跳转。下面是一个简单的例子,展示了如何在两个Fragment之间进行导航:
1.首先,创建一个Navigation Graph文件。在你的项目中的
  1. nav_graph.xml
复制代码
文件中,定义你的目的地和路径
  1. <navigation xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:app="http://schemas.android.com/apk/res-auto"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:id="@+id/nav_graph"
  5.     app:startDestination="@+id/fragment_home">
  6.     <fragment
  7.         android:id="@+id/fragment_home"
  8.         android:name=".ui.fragments.HomeFragment"
  9.         android:label="Home">
  10.         <action
  11.             android:id="@+id/action_home_to_about"
  12.             app:destination="@id/fragment_about"/>
  13.     </fragment>
  14.     <fragment
  15.         android:id="@+id/fragment_about"
  16.         android:name=".ui.fragments.AboutFragment"
  17.         android:label="About">
  18.     </fragment>
  19. </navigation>
复制代码
在这个例子中,我们有两个目的地:HomeFragment和AboutFragment。HomeFragment有一个到AboutFragment的跳转动作(action)。
2.在你的Activity中,获取NavController并启动动作。在你的Activity或Fragment的代码中,使用以下代码:
  1. // 获取NavController
  2. private lateinit var navController: NavController
  3. val navGraph = Navigation.findNavController(this, R.id.nav_host_fragment) as NavController?
  4. // 启动动作
  5. val destination = navGraph.destinationForId(R.id.action_home_to_about)?.let { it as? Destination } ?: Destination() // 使用你的动作ID替换R.id.action_home_to_about
  6. val result = navController.navigate(destination) // 导航到AboutFragment
复制代码
这样就成功地在两个Fragment之间进行了导航。当用户点击按钮或其他触发条件时,这个导航动作就会被启动。此外,跳转动作应该在Navigation Graph文件中定义。
到此这篇关于Android中的导航navigation的使用的文章就介绍到这了,更多相关Android 导航navigation内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
      1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
      2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
      3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
      4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
      5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
21
积分
22
注册时间
2022-12-24
最后登录
2022-12-24

发表于 2025-3-11 16:30:18 | 显示全部楼层
顶顶更健康!!!
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
17
积分
14
注册时间
2022-12-24
最后登录
2022-12-24

发表于 2025-7-17 13:15:12 | 显示全部楼层
感谢楼主,顶。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1楼
2楼
3楼

手机版|晓枫资讯--科技资讯社区 本站已运行

CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

晓枫资讯--科技资讯社区

本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表