jetpack 组件
As you know, most Android apps comprise multiple screens through, which the user navigates using screen gestures, button clicks and menu selections. Prior to the introduction of Android Jetpack components, the implementation of navigation within an app was largely a manual coding process with no easy method to view and organize potentially complex navigation paths. This condition has been enhanced considerably by introducing Android Jetpack Navigation Component. This essay aims to discuss some main concepts in using Android Jetpack Navigation Components for Android development.
如您所知,大多数Android应用程序都包含多个屏幕,用户可以使用屏幕手势,按钮单击和菜单选择进行浏览。 在引入Android Jetpack组件之前,应用内导航的实现很大程度上是手动编码过程,没有简单的方法来查看和组织可能复杂的导航路径。 通过引入Android Jetpack导航组件,已大大改善了这种情况。 本文旨在讨论使用Android Jetpack导航组件进行Android开发的一些主要概念。
简介与概述 (Introduction and Overview)
Basically, navigation refers to the interactions, which allow users to navigate across, into, and back out from the various pieces of content within your Android app. In other words, navigating around an app, moving from one screen to another, is an absolutely fundamental part of Android development. Historically, you will perform this using intents or fragment transactions like clicking a bottom. However, what if you wanted to do something just slightly more complex? For instance, to work with a common pattern like bottom navigation, you will require to make sure that not only Bottom Navigation view navigates, but also that it highlights the correct button. In addition, it manages the backstack in a uniform way. Therefore, that users do not end up getting lost or confused in this process. Android Jetpack’s Navigation Component helps you implement navigation, from simple button clicks to more complex patterns, such as app bars and the navigation drawer. To be more specific, the Navigation component is a collection of libraries, a plug-in, and tooling that simplifies Android navigation. Beyond making the setup for common patterns like Bottom Navigation easier, the component handles the backstack, fragment transactions, argument passing, navigation-based animations, and deep linking. Most importantly, it can gather up all of this navigation information, and can put it in one visualized location of your app via the Navigation graph. In fact, the Navigation component includes three main parts: Navigation graph, NavHost, and NavController.
基本上,导航是指交互,允许用户在Android应用程序中的各种内容之间导航,浏览和返回。 换句话说,在一个应用程序中浏览,从一个屏幕切换到另一个屏幕,是Android开发中绝对必要的部分。 从历史上看,您将使用意图或片段事务(例如单击底部)来执行此操作。 但是,如果您想做一些稍微复杂的事情怎么办? 例如,要使用底部导航之类的通用模式,您不仅需要确保底部导航视图能够导航,还需要确保其突出显示正确的按钮。 另外,它以统一的方式管理后栈。 因此,用户不会在此过程中迷路或迷路。 Android Jetpack的导航组件可帮助您实现导航,从简单的按钮单击到更复杂的模式,例如应用程序栏和导航抽屉。 更具体地说,导航组件是库,插件和工具的集合,可简化Android导航。 除了简化诸如底部导航之类的常见模式的设置之外,该组件还处理后退栈,片段事务,参数传递,基于导航的动画和深度链接。 最重要的是,它可以收集所有这些导航信息,并可以通过“导航”图将其放置在应用程序的一个可视化位置。 实际上,导航组件包括三个主要部分:导航图,NavHost和NavController。
导航组件的主要概念 (The main concepts of Navigation Component)
The Navigation Component can work properly with fragments and activities. Besides, you can be able to extend the library to work with other screen implementations like your custom view. In short, there are three main parts working together in harmony for accomplishing Navigation component purposes . These are the Navigation graph, the NavHost, and the NavController.
导航组件可以正确处理片段和活动。 此外,您可以扩展该库以使其与其他屏幕实现(例如自定义视图)一起使用。 简而言之,共有三个主要部分协同工作以实现导航组件的目的。 这些是导航图,NavHost和NavController。
导航图 (The Navigation graph)
The Navigation graph is a new resource type. It is an XML file that includes and centralizes information related to navigation. Currently, this information could be visualized in the new Navigation editor that is available in Android Studio 3.3. This means it is a graphical editor to create this Navigation graph. Each of these screens is called destinations, or places that you can navigate to. For instance, they are all fragments in the below picture:
导航图是一种新的资源类型。 这是一个XML文件,其中包含并集中了与导航有关的信息。 当前,此信息可以在Android Studio 3.3中可用的新导航编辑器中可视化。 这意味着它是创建此导航图的图形编辑器。 这些屏幕中的每一个都称为Destinations或可以导航到的地方。 例如,它们都是下图中的片段:
So, these arrows here are called actions and they represent the various paths that a user could take through your app. If you click on one of the actions, you can observe a whole bunch of embedded information, including data passed among destinations, transition animations, backstack manipulation, and so on. Clicking on a destination, you can see things like deep link and URLs and launch options.
因此,这些箭头在这里称为动作 ,它们代表用户可以通过您的应用程序使用的各种路径。 如果单击其中一项操作,则可以观察到一大堆嵌入的信息,包括在目标之间传递的数据,过渡动画,后退堆栈操作等。 单击目标,您可以看到诸如深层链接,URL和启动选项之类的信息。
To add a navigation graph to your project, you should follow these steps:
要将导航图添加到您的项目,您应该按照以下步骤操作:
- In the Project window, select New > Android Resource File. The New Resource File dialog appears. 在项目窗口中,选择新建> Android资源文件。 出现“新资源文件”对话框。
- Type a name in the File name field. 在文件名字段中输入名称。
- Select Navigation from the Resource type drop-down list, and then click OK. 从资源类型下拉列表中选择导航,然后单击确定。
Navigation graph: An XML resource that contains all navigation-related information in one centralized location. This includes all of the individual content areas within your app, called destinations, as well as the possible paths that a user can take through your app.
导航图:一种XML资源,在一个集中位置包含所有与导航有关的信息。 这包括您应用程序中的所有单个内容区域(称为destinations) ,以及用户可以通过您的应用程序访问的可能路径。
导航主机 (NavHost)
A NavHost is simply a special fragment (NavHostFragment) that is embedded into the user interface layout of an activity and serves as a placeholder for the destinations through which the user will navigate. In other words, this is a fragment widget that you will add to your layout, assuming that you are doing fragment navigation. It is basically a window that swaps in and out various fragment destinations that are included in your Navigation graph.
NavHost只是一个特殊的片段( NavHostFragmen t),它嵌入到活动的用户界面布局中,并充当用户将通过其导航的目的地的占位符。 换句话说,这是一个片段小部件,假设您正在执行片段导航,您将添加到布局中。 基本上,这是一个窗口,可以交换出导航图中包含的各种片段目标。
NavHost
: An empty container that displays destinations from your navigation graph. The Navigation component contains a defaultNavHost
implementation,NavHostFragment
, that displays fragment destinations.
NavHost
:一个空的容器,用于显示导航图中的目的地。 导航组件包含一个默认的NavHost
实现NavHostFragment
,它显示片段目的地。
导航控制器 (NavController)
The final piece of the puzzle is the NavController. Each NavHostFragment has a NavController that you will use in your Kotlin or Java code. This is what actually instructs the navigation to occur. Thus, if I write some code like this, the NavController will then perform this navigation action based off all of that information that was in the Navigation graph, and it will ultimately swap out which fragment is shown in the NavHost fragment. For example, here it has been used the action’s XML ID to specify which of those action arrows to follow, but an even better way to do this is using the navigation component’s SafeArgs plug-in.
难题的最后一部分是NavController。 每个NavHostFragment都有一个NavController ,将在您的Kotlin或Java代码中使用。 这实际上是指示导航发生的原因。 因此,如果我编写这样的代码,则NavController将基于Navigation图表中的所有信息执行此导航操作,最终它将交换掉NavHost片段中显示的片段。 例如,这里已使用操作的XML ID来指定要遵循的操作箭头中的哪一个,但是更好的方法是使用导航组件的SafeArgs插件。
viewTransactionsButton.setOnClickListener { view ->
view.findNavController().navigate(R.id.viewTransactionsAction)
}
NavController
: An object that manages app navigation within aNavHost
. TheNavController
orchestrates the swapping of destination content in theNavHost
as users move throughout your app.
NavController
:管理NavHost
应用程序导航的NavHost
。 当用户在整个应用程序中移动时,NavHost
会在NavController
协调目标内容的交换。
SafeArgs插件 (SafeArgs Plugin)
As a matter of fact, the SafeArgs plug-in generates code to allow you to use type-safe navigation and argument passing. First of all, you should add the Gradle plug-in to your code. This will generate classes based on your Navigation graph. It will make these direction classes for any destination that has actions, and it will make Args classes for any destination with arguments. Instead of referring to an action by its XML ID, you are making sure that you have an action that is associated with your destination. Also, you can be able to set arguments of the action. Another point is that you can pass in the wrong type, but the code will not be compiled. As you can see, you can Simply use the GeneratedArgs class, and you can have type-safe access to only the correctly-named arguments. Finally, there is no need to worry about the key-value pairs in your code.
实际上,SafeArgs插件生成代码以允许您使用类型安全的导航和参数传递。 首先,您应该将Gradle插件添加到您的代码中。 这将基于您的导航图生成类。 它将为具有操作的任何目的地创建这些方向类,并且为具有参数的任何目的地创建Args类。 请确保没有与目标关联的操作,而不是通过其XML ID引用操作。 另外,您可以设置操作的参数。 另一点是您可以传递错误的类型,但是代码不会被编译。 如您所见,您可以简单地使用GeneratedArgs类,并且可以仅对正确命名的参数进行类型安全的访问。 最后,无需担心代码中的键值对。
使用NavigationUI (Using NavigationUI)
The navigation component includes an additional navigation UI library in Java and ktx extensions for Kotlin with support for: Options Menus, Bottom Navigation, Navigation View, and Navigation Drawer. It also works with Action Bar, Toolbar, and Collapsing Toolbar.
导航组件包括Java和ktx扩展的Kotlin附加导航UI库,并支持:选项菜单,底部导航,导航视图和导航抽屉。 它还可以与操作栏,工具栏和折叠工具栏一起使用。
The Navigation component includes a
NavigationUI
class. This class contains static methods that manage navigation with the top app bar, the navigation drawer, and bottom navigation.导航组件包括
NavigationUI
类。 此类包含使用顶部应用程序栏,导航抽屉和底部导航管理导航的静态方法。
For example, When a user selects a menu item, the NavController
calls onNavDestinationSelected()
and automatically updates the selected item in the bottom navigation bar.
例如,当用户选择菜单项时, NavController
调用onNavDestinationSelected()
并自动更新底部导航栏中的所选项目。
To create a bottom navigation bar in your Android application, firstly, you must define the bar in your main activity as follows:
要在Android应用程序中创建底部导航栏,首先,必须在主活动中定义栏,如下所示:
...
android:id="@+id/nav_host_fragment"
... />
android:id="@+id/bottom_nav"
app:menu="@menu/menu_bottom_nav" />
Then, you should call setupWithNavController()
from your main activity's onCreate()
method as follows:
然后,应从主活动的onCreate()
方法调用setupWithNavController()
,如下所示:
override fun onCreate(savedInstanceState: Bundle?) {
setContentView(R.layout.activity_main)
...
val navController = findNavController(R.id.nav_host_fragment)
findViewById(R.id.bottom_nav)
.setupWithNavController(navController)
}
This one line of code has NavController manage the actual navigation, and it makes sure to follow material design best practices as far as backstack management goes. Most importantly, it handles highlighting the correct bottom buttons.
这段代码由NavController管理 实际的导航,并确保在回溯管理方面遵循材料设计最佳实践。 最重要的是,它可以突出显示正确的底部按钮。
导航组件的优势 (Navigation Component Advantages)
So, as you can see, this component has some prominent benefits for developing an Android app as follows:
因此,如您所见,该组件具有如下开发Android应用程序的显着优势:
- Simplified setup for common navigation patterns. 通用导航模式的简化设置。
2. Manages backstack.
2.管理堆栈。
3. Automates fragment transactions.
3.自动执行碎片交易。
4. Type safe argument passing.
4.键入安全参数传递。
5. Manages transition animations.
5.管理过渡动画。
6. Simplified deep linking.
6.简化的深层链接。
7. Centralizes and visualizes navigation.
7.集中并可视化导航。
8. ViewModel support.
8. ViewModel支持。
结论 (In conclusion)
Prior to the introduction of Android Jetpack components, the implementation of navigation within an Android app was largely a manual coding process with no easy method to view and organize potentially complex navigation paths. This condition has been enhanced considerably by introducing Android Jetpack Navigation Component. This essay aims to considered some main concepts in using Android Jetpack Navigation Components.
在引入Android Jetpack组件之前,Android应用程序中的导航实现主要是手动编码过程,没有简单的方法来查看和组织可能复杂的导航路径。 通过引入Android Jetpack导航组件,已大大改善了这种情况。 本文旨在考虑使用Android Jetpack导航组件的一些主要概念。
翻译自: https://medium.com/kayvan-kaseb/using-android-jetpack-navigation-component-a3ed8ce4c8e8
jetpack 组件