Android官方推荐架构组件Navigation 让单 Activity 应用成为首选架构,更好的管理Fragment框架android
Navigation目前仅AndroidStudio 3.2以上版本支持,若是您的版本不足3.2, 下载AndroidStudio3.2以上版本。
官网下载地址:https://developer.android.google.cn/studio/git
快速开发, 组件可单独使用,也能够同时工做。 消除样板 ,让代码Android 架构Jetpack管理乏味的活动事件,好比后台任务、导航和生命周期管理。这样你能够专一于让你的app更棒的东西,构建高质量、健壮的app 基于现代设计实践,Android Jetpack组件能够减小崩溃和内存泄漏,且向后兼容。接下来说述Navigation的使用以及如何管理多个Fragment等github
项目builde.gradle文件需配置:架构
implementation "androidx.navigation:navigation-fragment-ktx:2.3.0-alpha05"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0-alpha05"app
1.建立MainActivity和布局文件activity_navigation布局里配置: 框架
activity_navigation.xml布局配置:ide
<fragment android:id="@+id/my_nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" app:defaultNavHost="true" app:navGraph="@navigation/mobile_navigation" /> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_nav_view" android:layout_width="match_parent" android:layout_height="wrap_content" app:labelVisibilityMode="labeled" app:menu="@menu/bottom_nav_menu" />
MainActivity里配置:布局
val host: NavHostFragment = supportFragmentManager .findFragmentById(R.id.my_nav_host_fragment) as NavHostFragment? ?: return val navController = host.navController val bottomNav = findViewById<BottomNavigationView>(R.id.bottom_nav_view) bottomNav?.setupWithNavController(navController)
二、新建HomeFragment、FlowStepFragment、FlowStepFragment、SettingsFragment、DeepLinkFragment在Fragment片断里跳片断方法gradle
方法1: view.findViewById(R.id.navigate_destination_button).setOnClickListener( Navigation.createNavigateOnClickListener(R.id.next_action) )ui
方法2: view.findViewById(R.id.navigate_destination_button)?.setOnClickListener { findNavController().navigate(R.id.flow_step_one_dest, null, null) }
三、在res里新建文件夹navigation 类型选择Navigation,而后在这个文件夹里建立mobile_navigation.xml
在mobile_navigation.xml里写入要跳转的各个片断Fragment及要传递的参数: startDestination默认第一个跳的片断id
destination跳到另一个片断id
action 隐式跳转 ,argType传递参数类型,defaultValue传递参数值
<?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"
app:startDestination="@+id/home_dest">
<fragment android:id="@+id/home_dest" android:name="com.my.navigation.HomeFragment" android:label="@string/home" tools:layout="@layout/home_fragment"> <!--todo destination隐式跳转到Fragement id=flow_step_one_dest --> <action android:id="@+id/next_action" app:destination="@+id/flow_step_one_dest" app:enterAnim="@anim/slide_in_right" app:exitAnim="@anim/slide_out_left" app:popEnterAnim="@anim/slide_in_left" app:popExitAnim="@anim/slide_out_right" /> </fragment> <fragment android:id="@+id/flow_step_one_dest" android:name="com.my.navigation.FlowStepFragment" tools:layout="@layout/flow_step_one_fragment"> <argument android:name="flowStepNumber" app:argType="integer" android:defaultValue="1"/> <action android:id="@+id/next_action" app:destination="@+id/flow_step_two_dest"> </action> </fragment> <fragment android:id="@+id/flow_step_two_dest" android:name="com.my.navigation.FlowStepFragment" tools:layout="@layout/flow_step_two_fragment"> <argument android:name="flowStepNumber" app:argType="integer" android:defaultValue="2"/> <action android:id="@+id/next_action" app:destination="@+id/settings_dest"> </action> </fragment> <fragment android:id="@+id/settings_dest" android:name="com.my.navigation.SettingsFragment" android:label="@string/settings" tools:layout="@layout/settings_fragment" > <action android:id="@+id/next_action" app:destination="@+id/deeplink_dest"> </action> </fragment> <fragment android:id="@+id/deeplink_dest" android:name="com.my.navigation.DeepLinkFragment" android:label="@string/deeplink" tools:layout="@layout/deeplink_fragment"> <argument android:name="myarg" android:defaultValue="Android!"/> </fragment>
</navigation>
<!--项目地址:https://github.com/Visen123/MyNavigation-->;