jetpack组件_Jetpack导航组件入门

jetpack组件

Navigation has always been a key component in every app, however, it has presented many challenges for Android developers to ponder over. Common among these challenges is fragment transactions, up/back navigation, passing arguments between scenes safely and securely, managing the back stack of an app and many more. That is why in 2018 Google announced the Navigation Architecture Component library. The navigation component forms part of a collection of libraries, tools, and guides known as Android Jetpack which is aimed at making android app development easy and quick.

导航一直是每个应用程序中的关键组件,但是,它给Android开发人员带来了许多挑战。 这些挑战中常见的是片段事务,向上/向后导航,在场景之间安全可靠地传递参数,管理应用程序的后向堆栈等等。 这就是为什么Google在2018年发布了Navigation Architecture Component库。 导航组件是称为Android Jetpack的库,工具和指南的集合的一部分,旨在使Android应用程序的开发变得轻松快捷。

In this article, I will be presenting my experience using the Navigation Component as part of my learnings. Let’s start by getting an overview of exactly what the library does:

在本文中,我将作为学习的一部分介绍我使用导航组件的经验。 让我们首先概述一下库的功能:

  1. It handles Fragment transaction

    它处理片段交易
  2. Simplifies handling Up/Back navigation

    简化上/后导航的处理
  3. Handles deep linking to different parts of the app

    处理与应用程序不同部分的深层链接
  4. Allows us to pass arguments in a safe and secure way

    允许我们以安全可靠的方式传递参数
  5. Handles back stack management in the app

    在应用程序中处理后退堆栈管理
  6. It simplifies testing navigation in the app

    它简化了应用程序中的测试导航
  7. Most of all it does all of this in a consistent way, which helps developers to follow Android’s principles of navigation.

    最重要的是,它以一致的方式完成所有这一切,这有助于开发人员遵循Android的导航原理。

This leads us to:

这导致我们:

导航原理 (The Principles of Navigation)

According to androids documentation on the principles of navigation, The Navigation component is designed to implement these principles by default, ensuring that users can apply the same heuristics and patterns in navigation as they move between apps. Let us take a look at each of these principles:

根据有关导航原理的android文档, 导航组件默认情况下设计为实现这些原理,以确保用户在应用之间移动时可以在导航中应用相同的启发式和模式。 让我们看一下这些原则:

1.固定的开始目的地 (1. Fixed start destination)

Every app has a fixed start destination. This is usually the first screen the user sees when they launch your app from the launcher. It is also the last screen they see when they click the back button before exiting your app. It is worth noting that apps that require login can have an exception to this rule since a user will usually not log in every time they are going to use your app.

每个应用都有固定的启动目标。 这通常是用户从启动器启动应用程序时看到的第一个屏幕。 这也是他们退出应用程序之前单击“后退”按钮时看到的最后一个屏幕。 值得注意的是,需要登录的应用程序可以对此规则有例外,因为用户通常不会在每次使用您的应用程序时登录。

2.导航状态显示为一堆目的地 (2. Navigation state is presented as a stack of destinations)

The navigation state of your app should be represented with a last in first out structure(LIFO). A typical navigation stack has the start destination at the bottom of the stack and the current destination at the top of the stack. Operations that change the navigation of the app should always operate at the top of the stack either by pushing a new destination to the top or popping the topmost destination from the stack.

应用的导航状态应使用后进先出(LIFO)表示。 典型的导航堆栈在堆栈底部具有起始目的地,而在堆栈顶部具有当前目的地。 更改应用程序导航的操作应始终在堆栈顶部进行操作,方法是将新的目标推入顶部或从堆栈中弹出最高的目标。

3.在您的应用程序任务中,“上”和“后”是相同的 (3. Up and Back are identical within your apps tasks)

The Up button in the ActionBar and the system Back button both work in the same way when navigating the history of screens a user has recently visited within your app. While the two buttons perform similar actions, it is important to note that only the Back button is used to navigate out of the app. The Up button, does not appear in the start destination of the app because it never exits the app (note that this is an effort to ensure consistency across android apps).

浏览用户最近在您的应用中访问过的屏幕的历史记录时,ActionBar中的“向上”按钮和系统的“后退”按钮都以相同的方式工作。 尽管这两个按钮执行类似的操作,但必须注意,只有“后退”按钮用于导航到应用程序之外。 “向上”按钮不会出现在应用程序的启动目标中,因为它永远不会退出应用程序(请注意,这是为了确保android应用程序之间的一致性)。

如何导航 (How to Navigate)

Now that we have covered the principles of navigation, let’s see how we will implement this in a project that has one activity and fragments.

既然我们已经介绍了导航原理,让我们看看如何在一个有一个活动和多个片段的项目中实现这一点。

1.将导航组件添加到项目 (1. Adding the Navigation Component to the project)

First, we need to add the navigation component to our project in the app’s build.gradle file.

首先,我们需要将导航组件添加到应用程序的build.gradle文件中的项目中。

dependencies {
implementation "android.arch.navigation:navigation-fragment- ktx:$version_navigation"
implementation "android.arch.navigation:navigation-ui- ktx:$version_navigation"
}

2.将导航图添加到项目 (2. Adding the Navigation Graph to the project)

The navigation graph is a resource file that contains all your app’s destinations, and actions that a user can take to navigate from one destination to another. To add it:

导航图是一个资源文件,其中包含您应用程序的所有目标,以及用户可以从一个目标导航到另一个目标的操作。 要添加它:

right-click on the res directory in your project window and select New > Android resource file. The New Resource dialog appears. Select Navigation as the resource type and name the file navigation. This creates a navigation.xml file which serves as your navigation graph.

右键单击项目窗口中的res目录,然后选择“新建”>“ Android资源文件”。 出现“新资源”对话框。 选择Navigation作为资源类型,然后将文件命名为Navigation 。 这将创建一个navigation.xml文件作为您的导航图。

Image for post
creating the navigation graph resource file
创建导航图资源文件

3.将Navigation Host片段放入“活动布局”中。 (3. Put Navigation Host fragment in your Activity Layout.)

Now go to your activity layout (eg. activity_main.xml) and include your container fragment. Give it an ID navHostFragment, set the name attribute the name of the fragment and the app:navGraph attribute to @navigation/navigation and then set defaultNavHost = true, this allows the navHost to intercept the system back key.

现在转到您的活动布局(例如activity_main.xml),并包含您的容器片段。 给它一个ID navHostFragment,将片段的名称属性name和app:navGraph属性设置为@ navigation / navigation,然后将defaultNavHost = true设置,这将使navHost可以截获系统后退键。

<fragment
android:id="@+id/navHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="@navigation/navigation"
app:defaultNavHost="true"/>

4.将片段添加到导航图 (4. Adding Fragments to the Navigation Graph)

Now that your host fragment is set up, it is time to add fragments to it. This is has been made very simple by using the navigation editor. To do this, open the design view of your navigation editor, click on the little plus like button in the left corner, and from the list that appears, choose the destination you want to add to the navigation graph.

现在,您的主机片段已设置完毕,是时候向其中添加片段了。 使用导航编辑器可以使此操作非常简单。 为此,请打开导航编辑器的设计视图,单击左上角的小加号按钮,然后从出现的列表中选择要添加到导航图的目标。

Image for post
Adding destinations(fragments) to the navigation graph
将目的地(片段)添加到导航图

5.用动作连接两个片段 (5. Connecting Two Fragments with an Action)

To connect FragmentA and FragmentB, hover over FragmentA. You’ll see a circular connection point on the right side of the fragment view. Click on the connection point and drag it to FragmentB to add an Action that connects the two fragments.

要连接FragmentA和FragmentB,请将鼠标悬停在FragmentA上。 您会在片段视图的右侧看到一个圆形的连接点。 单击连接点并将其拖到FragmentB,以添加连接两个片段的Action。

Image for post
Connecting two fragments with Actions.
用动作连接两个片段。

6.使用按钮从FragmentA导航到FragmentB (6. Navigating from FragmentA to FragmentB using a Button)

Now to navigate between these two fragments using a button, set an onClick listener on the button and then using the Navigation’s onClick listener to implement it as follows.

现在,要使用按钮在这两个片段之间导航,请在按钮上设置一个onClick侦听器,然后使用导航的onClick侦听器来实现它,如下所示。

binding.button.setOnClickListener(
Navigation.createNavigateOnClickListener(R.id.action_FragmentA_to_FragmentB))

And with this, you should successfully navigate from one fragment to another.

这样,您就可以成功地从一个片段导航到另一个片段。

To further read on this topic, here are the reference materials I used:

为了进一步阅读该主题,以下是我使用的参考资料:

翻译自: https://medium.com/swlh/getting-started-with-the-jetpack-navigation-component-99d1fc2fbc1e

jetpack组件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Jetpack是一套基于Android开发语言Kotlin的组件库,目的是帮助Android开发者快速构建健壮的、高效的应用程序。Jetpack架构组件入门到精通是一本介绍Jetpack组件库的电子书,其内容向读者提供了Jetpack组件库的最基础的知识体系,以及对组件库的更高阶的实践知识和技能分享。 在读者学习这本电子书时,首先会学习到基本的Jetpack概念,并了解到如何使用各种工具来实现底层代码、布局和资源文件的编写。同时,电子书重点讲解了Jetpack组件库中最常用的组件,比如ViewModel、LiveData、Room、WorkManager等,它们各自的功能和用法,让读者熟知Jetpack组件的基本运作原理。读者还会学习如何将这些组件综合使用,以构造真正的应用程序。在内容的后半部分,电子书重点讲解了Jetpack在架构层面的设计,以及如何更有效的使用和管理组件库。 总之,Jetpack架构组件入门到精通这本电子书非常适合想要学习Jetpack组件库的Android初学者和有一定开发经验的开发者,能够帮助他们快速掌握Jetpack组件库,以及提高软件的可扩展性和有效性。 ### 回答2: Jetpack架构组件是一组Android软件组件和库,通过这些组件和库,开发者可以更加轻松地开发应用程序,并提供高质量的用户体验。Jetpack架构组件包括众多不同的组件和库,这些组件和库都具有不同的用途和功能。对于开发者来说,学习Jetpack架构组件是非常重要的。 《jetpack架构组件入门到精通 pdf》是一份针对Jetpack架构组件的学习资料。它是可以帮助开发者更好地理解和掌握Jetpack架构组件的重要手册。通过这份PDF文档,开发者可以学习到Jetpack架构组件的基本概念、用途和功能,并了解如何将这些组件和库应用到他们的应用程序开发中。 这份PDF文档涵盖了Jetpack架构组件的多个方面,包括ViewModel、LiveData、Data Binding、Room、Navigation、WorkManager等等。每个章节都包含了详细的介绍和具体的用例,从而帮助开发者更好地理解每个组件和库的用途和功能。 无论是初学者还是经验丰富的开发者,都可以从这份PDF文档中获益。它可以帮助初学者更轻松地入门Jetpack架构组件,并提供了有用的工具和技巧。对于经验丰富的开发者来说,这份PDF文档可以帮助他们更深入地了解Jetpack架构组件,并提供了一些高级技术和策略,以优化应用程序性能和用户体验。 总之,Jetpack架构组件入门到精通的PDF文档是一份非常有价值的资源,它可以帮助开发者更好地理解和掌握Jetpack架构组件,从而更加轻松地开发高质量的Android应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值