Navigation
版本导入:
ext.nav_version = "2.3.5"
implementation("androidx.navigation:navigation-fragment-ktx:$nav_version")
implementation("androidx.navigation:navigation-ui-ktx:$nav_version")
NavHost
navigation的宿主载体,即需要一个空的布局来承载fragment的切换。
navigation的宿主需要实现NavHost接口,该接口仅一个接口需要实现getNavController(),返回一个NavController,这个NavController应该就是实现navigation组件管理fragment的关键,后续再去针对它进行深入学习。
navigation包内已经提供了一个实现了NavHost接口的fragment(androidx.navigation.fragment.NavHostFragment),所以在使用过程中可以直接使用它来作为navigation的宿主载体
public interface NavHost {
/**
* Returns the {@link NavController navigation controller} for this navigation host.
*
* @return this host's navigation controller
*/
@NonNull
NavController getNavController();
}
如下,为APP创建一个MainActivity,作为程序的入口,在activity的layout中添加navigation的NavHost,xml布局如下:
<?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">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/host_fragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/navigation_graph"
app:defaultNavHost="true"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
FragmentContainerView中添加NavHostFragment
主要关注以下几个属性:
name:指定实现了NavHost接口的实现类,即navigation的宿主
navGraph:导航图,指定了各个fragment之间跳转切换的路径,xml文件,往下会在具体介绍
defaultNavHost:该属性用来控制了NavHostFragment是否会进行返回键事件的拦截处理
在实际使用中发现还有一个地方需要注意,需要为该FragmentContainerView指定id,否则会出现异常
NavGraph
导航图:res/navigation下的xml文件。
包含了所有目标页面和执行动作,能清晰的看出所有导航页面的跳转路径。
如上图所示:
该导航图表示:MainFragment为默认其实节点页面,两个Fragment之间连接的箭头表示MainFragment包含的action,导航到Second Fragment
对应的navigation 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/navigation_graph"
app:startDestination="@id/mainFragment">
<--startDestination指定了起始fragment-->
<fragment
android:id="@+id/mainFragment"
android:name="com.example.navigationapp.fragment.MainFragment"
tools:layout="@layout/fragment_main">
<action
android:id="@+id/action_mainFragment_to_secondFragment"
app:destination="@id/secondFragment" />
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.example.navigationapp.fragment.SecondFragment"
android:label="SecondFragment"
tools:layout="@layout/fragment_second"
/>
</navigation>
很容易理解:
fragment标签表示导航图中包含的fragment。
name为对应fragment,tools:layout可以方便在导航图中预览对应的fragment默认界面
action即对应fragment下的可操作,id定义action的操作名称,destination指明要导航到的目的页的id。
到这里一个简单的导航图就完成了。
添加导航目标节点
如图,Android studio的navigation xml文件的Design图形化编辑界面可以直接点击左上角ADD按钮直接添加,可以直接添加一个现有的fragment,或者选择create new destination创建一个新的fragment进行添加。
如需参考代码示例可以参考NavGraph介绍部分。
添加完导航目标节点后,拖动目标节点页面的action点箭头连接下一个目标节点即可完成一个导航action操作的添加。
如何从一个节点导航到下一个节点,实现页面跳转?
如上MainFragment如何跳转到SecondFragment
前面提到要实现Navigation管理Fragment的跳转,需要一个宿主载体NavHost,该接口返回了一个NavController,该对象就是实现Fragment的管理、跳转的。
eg:先看一下怎么在MainFragment内获取到NavController
/**
* NavHostFragment提供了如下静态方法来获取NavController
*/
public static NavController findNavController(@NonNull Fragment fragment) {
Fragment findFragment = fragment;
/**first*/
while (findFragment != null) {
if (findFragment instanceof NavHostFragment) {
return ((NavHostFragment) findFragment).getNavController();
}
Fragment primaryNavFragment = findFragment.getParentFragmentManager()
.getPrimaryNavigationFragment();
if (primaryNavFragment instanceof NavHostFragment) {
return ((NavHostFragment) primaryNavFragment).getNavController();
}
findFragment = findFragment.getParentFragment();
}
/**
* 第一次获取,通过fragment不断循环向上寻找,直到找到某一个parent为NavHostFragment
* 时,通过NavHostFragment的getNavController获取到NavController
*/
// Try looking for one associated with the view instead, if applicable
View view = fragment.getView();
if (view != null) {
return Navigation.findNavController(view);
}
/**第二次通过view来获取,是什么情况呢,比如在activity时候则可以通过放置NavHostFragment
* 的view来获取,跟踪NavHostFragment onCreateView可以发现其会向view中添加id为
* nav_controller_view_tag的tag,因此也可以通过view来获取NavController
*/
// For DialogFragments, look at the dialog's decor view
Dialog dialog = fragment instanceof DialogFragment
? ((DialogFragment) fragment).getDialog()
: null;
if (dialog != null && dialog.getWindow() != null) {
return Navigation.findNavController(dialog.getWindow().getDecorView());
}
throw new IllegalStateException("Fragment " + fragment
+ " does not have a NavController set");
}
获取到NavController,我们就可以来实现fragment的切换,如下:
/**通过NavController的navigation方法就可以实现action的跳转
* navigation有多种参数的重载,此处使用的是通过action的id实现的跳转
*/
getNavController().navigate(R.id.action_mainFragment_to_secondFragment)
通过上述重载方法可以看出,navigation,可以通过action id进行跳转,可以配置Uri进行跳转等,同时也可以通过bundle携带参数。上述的跳转都会在后续持续完善补充。