Navigation
去年的Google I/O大會上刊咳,Google推出了Android Jetpack架構(gòu)組件,Navigation就是其中之一。
本文主要介紹Navigation與BottomNavigationView的簡單搭配使用,最后的效果就是這樣:
注意:Android Studio版本需要在3.2及以上
一、創(chuàng)建Activity
上圖是在一個Activity中顯示了3個Fragment乖寒,第一步是創(chuàng)建這幾個Fragment的容器。
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context=".main.MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/navigation"/>
</LinearLayout>
fragment
-
android:name="androidx.navigation.fragment.NavHostFragment"
和app:defaultNavHost="true"
指定了院溺,在按back鍵時會返回到NavHostFragment
-
app:navGraph="@navigation/nav_graph"
為這個fragment
組件指定了navGraph
文件
BottomNavigationView
-
app:menu="@menu/navigation"
指定了底部導(dǎo)航欄的菜單配置選項 -
這里庫使用的是material而不是design楣嘁,是因為采用了AndroidX
遷移至AndroidX
二、配置menu文件
- 在
res
目錄下新建menu
文件夾 - 在
menu
目錄中珍逸,新建Menu resource file
- 在
text
標(biāo)簽中修改代碼:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/tab1"
android:icon="@mipmap/ic_launcher"
android:title="Tab 1"/>
<item
android:id="@+id/tab2"
android:icon="@mipmap/ic_launcher"
android:title="Tab 2"/>
<item
android:id="@+id/tab3"
android:icon="@mipmap/ic_launcher"
android:title="Tab 3"/>
</menu>
三逐虚、配置navigation graph
- 在
res
目錄下新建navigation
文件夾 - 在
navigation
目錄下,新建Navigation resource file
- 添加所需要導(dǎo)航的Fragment谆膳,注意不需要添加Action叭爱,不需要連接箭頭
- 將每個Fragment對應(yīng)的ID,修改為
menu
文件中item對應(yīng)的ID(最重要的一步)
最終的
nav_graph.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/nav_graph"
app:startDestination="@id/tab1">
<fragment
android:id="@+id/tab1"
android:name="..."
android:label="..."
tools:layout="@layout/fragment1">
</fragment>
<fragment
android:id="@+id/tab2"
android:name="..."
android:label="..."
tools:layout="@layout/fragment2" />
<fragment
android:id="@+id/tab3"
android:name="..."
android:label="..."
tools:layout="@layout/fragment3">
</fragment>
</navigation>
四漱病、Navigation與BottomNavigationView適配
在MainActivity.java
中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
BottomNavigationView bottomNav = findViewById(R.id.bottom_nav_view);
NavigationUI.setupWithNavController(bottomNav, navController);
}
@Override
public boolean onSupportNavigateUp() {
return Navigation.findNavController(this, R.id.nav_host_fragment);.navigateUp();
}
注意:
NavigationUI.setupWithNavController()
方法买雾,只有在onCreate()中調(diào)用才有效