BottomNavigationView的使用
BottomNavigationView和NavHostFragment配合使用可以做到切换Fragment的效果
1、首先编写布局文件
<?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">
<fragment
android:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:navGraph="@navigation/my_nav"
tools:ignore="FragmentTagUsage" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:menu="@menu/navigation" />
</LinearLayout>
2、创建navigation文件
如果是项目第一次创建navigation文件那么需要等待android studio加载相关依赖
随后,在可视化界面中将需要用到的fragment添加进来
最后得到的文件内容如下:
<?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/my_nav"
app:startDestination="@id/inspectFragment">
<fragment
android:id="@+id/projectFragment"
android:name="com.rfinspection.kjs.rfinspection.fragments.ProjectFragment"
android:label="@string/project"
tools:layout="@layout/fragment_project" />
<fragment
android:id="@+id/inspectFragment"
android:name="com.rfinspection.kjs.rfinspection.fragments.InspectFragment"
android:label="@string/inspect"
tools:layout="@layout/fragment_inspect" />
<fragment
android:id="@+id/maintainFragment"
android:name="com.rfinspection.kjs.rfinspection.fragments.MaintainFragment"
android:label="@string/maintain"
tools:layout="@layout/fragment_maintain" />
</navigation>
3、编写BottomNavigationView对应的menu文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/projectFragment"
android:icon="@drawable/vector_drawable_zg_building"
android:title="@string/project" />
<item
android:id="@+id/inspectFragment"
android:icon="@drawable/vector_drawable_inspect_record"
android:title="@string/inspect" />
<item
android:id="@+id/maintainFragment"
android:icon="@drawable/vector_drawable_repair"
android:title="@string/maintain" />
</menu>
这里需要注意的是menu文件中item的id要与navigation文件中fragment的id一一对应,保持一样。
4、在Activity中将BottomNavigationView与NavHostFragment关联起来
//获取NavHostFragment的navigation controller
val navController = findNavController(R.id.fragment)
//配置actionbar,actionbar的title以navigation文件中fragment的label为值
//val appBarConfiguration = AppBarConfiguration(setOf(R.id.projectFragment,R.id.inspectFragment,R.id.maintainFragment))
//setupActionBarWithNavController(navController,appBarConfiguration)
//将BottomNavigationView与NavHostFragment关联起来
bottomNavigation.setupWithNavController(navController)
//设置BottomNavigationView的初始选中值
bottomNavigation.selectedItemId = R.id.projectFragment