聲明:個人筆記,無參考學習價值
部分內(nèi)容摘抄自:
作者:七適散人
鏈接:http://www.reibang.com/p/d37f5132db3c
1.res-->Android Resource File-->navigation-->xxx.xml
2.xxx.xml中將需要的多個fragment進行管理,可以指定Actions,Argments
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main_tab_navigation"
xmlns:tools="http://schemas.android.com/tools"
?? app:startDestination="@id/homepageFragment">
<fragment
tools:layout="@layout/fragment_homepage"
<--如果配合bottomNavigationView和menu使用的話,這里的id要和menu的id相同-->
?? android:id="@+id/homepageFragment"
android:name="com.myself.mvpdemo.maintap.view.fragment.HomepageFragment"
android:label="HomepageFragment" >
<action
android:id="@+id/action_homepageFragment_to_qandAfragment"
app:destination="@id/qandAfragment" />
<argument
android:name="safeData"
app:argType="integer"
android:defaultValue="12" />
</fragment>
</navigation>
tools:layout="@layout/fragment_homepage" : fragment的layout
android:name="" fragment的路徑
3 在actiivty中的引用,定義一個fragment:
- 第一種:在xml文件中定義
<fragment
android:id="@+id/nav_fragment"
?? android:name="androidx.navigation.fragment.NavHostFragment"
?? app:navGraph="@navigation/main_tab_navigation"
?? app:defaultNavHost="true"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"/>
android:name 是 NavHostFragment,它實現(xiàn)了 NavHost,這是一個用于放置管理 destination 的空視圖杈绸。
app:navGraph 用于將這個 NavHostFragment 和 xxx.xml 關聯(lián)起來。
app:defaultNavHost 表示 NavHostFragment 可以攔截處理返回鍵翠肘。
- 第二種 通過代碼創(chuàng)建 NavHostFragment空盼,先修改 Activity 的 xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
... >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame_layout" />
</android.support.constraint.ConstraintLayout>
在activity中使用:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_navigation)
val finalHost = NavHostFragment.create(R.navigation.nav_graph)
supportFragmentManager.beginTransaction()
.replace(R.id.frame_layout, finalHost)
.setPrimaryNavigationFragment(finalHost) // 等價于 xml 中的 app:defaultNavHost="true"
.commit()
}
4.處理跳轉
跳轉通過 NavController 對象,它有三種獲取方法:
- NavHostFragment.findNavController(Fragment)
- Navigation.findNavController(Activity, @IdRes int viewId)
- Navigation.findNavController(View)
調用 NavController 的 navigate 方法執(zhí)行跳轉阱持,navigate 的參數(shù)可以是一個 destination(這里就是 fragment 在導航圖 nav_graph 中的 id)琐馆,也可以是 action 的 id规阀。
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
button.onClick {
// NavHostFragment.findNavController(this@FirstFragment)
// .navigate(R.id.action_nav_graph_first_fragment_to_nav_graph_second_fragment)
Navigation.findNavController(getView()!!)
.navigate(R.id.action_nav_graph_first_fragment_to_nav_graph_second_fragment)
}
}
5.添加跳轉動畫
可在可視化Design中操作,生成代碼:
<fragment
android:id="@+id/nav_graph_first_fragment"
android:name="pot.ner347.androiddemo.navigation.FirstFragment"
android:label="first"
tools:layout="@layout/fragment_first">
<action
android:id="@+id/action_nav_graph_first_fragment_to_nav_graph_second_fragment"
app:destination="@id/nav_graph_second_fragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />
</fragment>
支持 View 動畫和屬性動畫,enterAnim 和 exitAnim 是去往棧里添加一個 destination 時兩個 destination 的動畫啡捶,popEnterAnim 和 popExitAnim 是從棧里移除一個 destination 時的動畫姥敛。
6.傳遞數(shù)據(jù)
要跳轉到 SecondFragment,要往 SecondFragment 里帶數(shù)據(jù)瞎暑,在目的 Fragment 里添加 <argument>
<!-- nav_graph.xml -->
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
... >
<fragment
android:id="@+id/nav_graph_second_fragment"
android:name="pot.ner347.androiddemo.navigation.SecondFragment"
android:label="second"
tools:layout="@layout/fragment_second" >
<argument android:name="name" android:defaultValue="Max"/>
</fragment>
</navigation>
FirstFragment 添加數(shù)據(jù)
button.onClick {
val bundle = bundleOf("name" to "silas")
Navigation.findNavController(getView()!!)
.navigate(R.id.action_nav_graph_first_fragment_to_nav_graph_second_fragment, bundle)
}
SecondFragment 獲取數(shù)據(jù)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
arguments?.getString("name")?.let { toast("hello $it") }
return inflater.inflate(R.layout.fragment_second, container, false)
}
如果 FirstFragment 沒有帶數(shù)據(jù)彤敛,那么 SecondFragment 將收到默認值 “Max”。