Android官方推薦架構組件Navigation 讓單 Activity 應用成為首選架構,更好的管理Fragment框架
Navigation目前僅AndroidStudio 3.2以上版本支持,如果您的版本不足3.2, 下載AndroidStudio3.2以上版本。官網下載地址:https://developer.android.google.cn/studio/
快速開發, 組件可單獨使用,也可以同時工作。 消除樣板 ,讓代碼Android 架構Jetpack管理乏味的活動事件,比如后臺任務、導航和生命周期管理。這樣你可以專注于讓你的App更棒的東西,構建高質量、健壯的app 基于現代設計實踐,Android Jetpack組件可以減少崩潰和內存泄漏,且向后兼容。接下來講述Navigation的使用以及如何管理多個Fragment等
項目builde.gradle文件需配置:
implementation "androidx.navigation:navigation-fragment-ktx:2.3.0-alpha05"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0-alpha05"
1.創建MainActivity和布局文件activity_navigation布局里配置:
activity_navigation.xml布局配置:
<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)
2、新建HomeFragment、FlowStepFragment、FlowStepFragment、SettingsFragment、DeepLinkFragment在Fragment片段里跳片段方法
方法1: view.findViewById(R.id.navigate_destination_button).setOnClickListener( Navigation.createNavigateOnClickListener(R.id.next_action) )
方法2: view.findViewById(R.id.navigate_destination_button)?.setOnClickListener { findNavController().navigate(R.id.flow_step_one_dest, null, null) }
3、在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