问题描述
我想在片段A的顶部添加片段B,而不会破坏片段A的视图.我在导航库中看到的唯一选项是使用导航方法打开一个片段,但是如何确保将片段B添加到片段A的顶部,而不是被替换.
i want to add fragment B on top of fragment A without view of fragment A getting destroyed. Only option i see in navigation library is to use navigate method to open a fragment but how to ensure fragment B is added on top of fragment A, not replaced.
navigationController.navigate(R.id.B, bundle);
以及如何查找片段A的实例以便能够与之交互?
and how to find the instance of fragment A to be able to interact with it?
推荐答案
想知道以便澄清自己.导航到片段B时为什么不希望破坏片段A?
Will like to know so as to clarify myself. Why don't you want fragment A be destroyed when you navigation to fragment B?
猜想是否不想丢失片段A的状态,可以在 viewModel ,并在您的活动的onCreate(无论您确定片段的哪种生命周期方法都适合)中检索它.如果不是这个原因,plz会解释一下,以便为我们提供更好的环境.
Guess if you won't want to loose fragment A's state, you can initialize it in a viewModel and retrieve it in the onCreate of your activity( whichever lifecycle method of your fragment you determines sounds fit). If this wasn't the reason, plz do explain to provide us with a better context.
用于将片段交换出容器的导航库方法.这是导航库的简短摘要
For the navigation library way of swapping fragments out of the container.here's a brief summary of the navigation library
根据文档(我建议您阅读该文档因为这是一个冗长的过程,带有很多模糊的词.
As per the docs(which I recommend you should read because it's a lengthy procedure with a lot fuzzy words.
假设您至少使用Android Studio 3.3或更高版本,
Assuming you are using at least Android Studio 3.3 or higher,
1- 将导航库添加到项目中.
dependencies {
def nav_version = "2.0.0"
implementation "androidx.navigation:navigation-fragment:$nav_version" // For Kotlin use navigation-fragment-ktx
implementation "androidx.navigation:navigation-ui:$nav_version" // For Kotlin use navigation-ui-ktx
}
2-创建导航图.
要将导航图添加到项目中,请执行以下操作:
To add a navigation graph to your project, do the following:
在项目"窗口中,右键单击res目录,然后选择新建">"Android资源文件".
In the Project window, right-click on the res directory and select New > Android Resource File.
出现新资源文件"对话框.在文件名"字段中输入一个名称,例如"nav_graph".
The New Resource File dialog appears.Type a name in the File name field, such as "nav_graph".
从资源类型"下拉列表中选择导航",然后单击确定".
Select Navigation from the Resource type drop-down list, and then click OK.
3- 向活动添加导航主机
在您要进行交换的父活动的xml视图中.在这种情况下,其视图构成要在其中进行交换的容器视图的活动,请添加以下具有适当导航图名称的代码段.
within the xml view of the parent activity wherein you want the swapping to be taking place. The activities whose view poses the container view in which you wish to be swapping in this case, add the following code snippet with the appropriate navigation graph name.
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/my_navigation_graph_file_name" />
未将defaultNavHost属性设置为true,以向导航库指示应该接管后退按钮和向上按钮的处理,以便以直观的方式在应用程序内正确导航.
not the attribute defaultNavHost set to true to indicate to the navigation library that it should take over the handling of the back button and up button for proper navigation within the application in an intuitive manner.
6- 将目标添加到图形中.
有三种方法可以将目的地添加到导航图.您可以从现有片段(已经在问题上下文中交换的片段)或活动中创建目标,创建新的目标,或创建占位符以稍后用片段或活动替换.
There are three different ways to add destinations to your navigation graph. You can create a destination from an existing fragment( the fragments you are already swapping in the question context) or activity, create a new destination, or create a placeholder to later replace with a fragment or activity.
要使用导航编辑器添加新的目的地类型,请执行以下操作:
To add a new destination type using the Navigation Editor, do the following:
在导航编辑器中,单击新建目标"图标,然后单击创建新目标".
In the Navigation Editor, click the New Destination icon , and then click Create new destination.
在出现的新建Android组件"对话框中,创建片段.有关片段的更多信息,请参见片段文档.
In the New Android Component dialog that appears, create your fragment. For more information on fragments, see the fragment documentation.
7- 将目的地指定为开始目的地
一旦所有目的地都准备就绪,您可以通过执行以下操作来选择起始目的地:
Once you have all of your destinations in place, you can choose a start destination by doing the following:
在设计"选项卡中,单击目标以突出显示它.
In the Design tab, click on the destination to highlight it.
右键单击目标,然后单击设置为起始目标".
Right-click on the destination and click Set as Start Destination.
8- 连接目的地
-
在设计"选项卡中,将鼠标悬停在您要用户导航的目标的右侧.目的地的右侧会出现一个圆圈.
In the Design tab, hover over the right side of the destination that you want users to navigate from. A circle appears over the right side of the destination.
在您希望用户导航到并释放的目标上单击并拖动光标.两个目标之间的结果线代表一个动作.
Click and drag your cursor over the destination you want users to navigate to, and release. The resulting line between the two destinations represents an action.
单击箭头以突出显示操作.以下属性出现在属性"面板中:
Click on the arrow to highlight the action. The following attributes appear in the Attributes panel:
类型"字段包含操作".ID字段包含操作的ID.目标"字段包含目标片段或活动的ID.
The Type field contains "Action".The ID field contains the ID for the action.The Destination field contains the ID for the destination fragment or activity.
单击文本"选项卡以切换到XML视图.现在将动作元素添加到源目标.该操作具有一个ID和一个目标属性,该属性包含下一个目标的ID,如下图所示:
Click the Text tab to toggle to the XML view. An action element is now added to the source destination. The action has an ID and a destination attribute that contains the ID of the next destination, as shown in the image following:
9- 导航到目的地
使用NavController(可在NavHost中管理应用程序导航的对象)完成到目的地的导航.每个NavHost都有自己的对应NavController.
Navigating to a destination is done using a NavController, an object that manages app navigation within a NavHost. Each NavHost has its own corresponding NavController.
要获取片段,活动或视图的NavController,请使用以下方法之一:
To retrieve the NavController for a fragment, activity, or view, use one of the following methods:
NavHostFragment.findNavController(Fragment)
Navigation.findNavController(Activity, @IdRes int viewId)
Navigation.findNavController(View)
检索到NavController后,请使用其Navigation()方法导航到目标位置. navigation()方法接受操作或目标的资源ID.
Once you retrieve a NavController, use its navigate() method to navigate to a destination. The navigate() method accepts a resource ID of either an action or a destination.
button.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.next_fragment_destination_name, null));
导航库必须为我们提供更多精彩的东西,以便我们为Android开发者提供服务.避免冗长的答案,这就是为什么我没有包含所有内容的原因.建议您整体查看导航库和android体系结构组件的官方文档.希望这会有所帮助.
and many more wonderful things the navigation library has to offer us android devs. Avoiding to make the answer very lengthy that's why I didn't include everything. Recommend you checkout the official doc for navigation library and android architecture components as a whole. Hope this helps.
这篇关于使用导航架构组件时如何与其他片段进行交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!