首先说一下这个的实现效果相似咱们如今实现的方式ViewPager切换fragment,咱们能够更简单,更优雅的实现咱们的目标,毕竟Google推出的框架嘛,感受仍是不错的,我的感受仍是得看一下官方文档,虽然是英文,不过一点点翻译呗,每一步都讲得蛮细的。android
首先配置一下gradle:web
implementation 'android.arch.navigation:navigation-fragment:1.0.0' implementation 'android.arch.navigation:navigation-ui:1.0.0' implementation 'com.android.support:design:28.0.0' implementation 'com.android.support:appcompat-v7:28.0.0' implementation "com.android.support:support-v4:28.0.0"
接下来咱们就可使用相关的api啦!api
首先咱们新建一个activity,叫MainActivity,布局文件以下:app
<fragment android:id="@+id/nav_host_fragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/navigation" app:defaultNavHost="true" app:navGraph="@navigation/nav_graph" /> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/navigation" android:layout_width="match_parent" android:layout_height="56dp" android:layout_alignParentBottom="true" app:menu="@menu/my_navigation_items" />
首先咱们看到咱们切换的页卡view是BottomNavigationView ,上面换成了fragment角标,这里的id能够随意给,name咱们必须声明成androidx.navigation.fragment.NavHostFragment ,否则找不到映射的fragment。 框架
这里的defaultNavHost属性为true,要求咱们在跳转到下一个fragment以后,返回键的相应是回到上一个fragment这个和webView的histtory是同样的。ide
重点是navGraph这个属性,咱们定义这个navigation文件:布局
如上图所示,新建这个navigation文件夹的过程也很简单:gradle
而后输入名字,肯定便可。ui
这样咱们就能够在布局里面作一些咱们本身要作的事情了,个人文件以下:this
你们看到了里面包含了三个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/nav_graph" app:startDestination="@id/blankFragment"> <fragment android:id="@+id/blankFragment" android:name="com.example.jespactdemo.BlankFragment" android:label="fragment_blank" tools:layout="@layout/fragment_blank" /> <fragment android:id="@+id/secondFragment" android:name="com.example.jespactdemo.SecondFragment" android:label="secondfragment" tools:layout="@layout/secondfragment" /> <fragment android:id="@+id/mineFragment" android:name="com.example.jespactdemo.MineFragment" android:label="fragment_mine" tools:layout="@layout/fragment_mine" /> </navigation>
除了最外层的角标不同以外,其他的跟咱们平时写的xml很相似,咱们声明了fragment,指明了预览layout,这里的id咱们先记着很重要,后面我再说。
咱们发如今navigation里面有一个属性startDestination,咱们指明了blankFragment,再看看design的时候,blankFragment预览图左上角有一个小房子的图案:
这说明咱们activity第一次进来的时候关联显示的fragment就是它了。
接下来咱们再看看底部切换布局怎么实现,首先咱们建立一个menu文件夹,而后在建立一个menu的xml,里面的内容以下:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/blankFragment" android:title="消息" android:icon="@mipmap/ic_launcher" /> <item android:id="@+id/secondFragment" android:title="首页" android:icon="@mipmap/ic_launcher" /> <item android:id="@+id/mineFragment" android:title="个人" android:icon="@mipmap/ic_launcher" /> </menu>
这里很简单,你们都能看得懂,这里咱们注意一下这里的id,以前我说过建立fragment的时候声明的id也很重要,这二者的有什么关系呢?关系就是咱们必定要一一对应,彻底同样才能够造成映射关系。
最后咱们看一下MainActivity的实现:
mBottomNav = findViewById(R.id.navigation); NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); NavigationUI.setupWithNavController(mBottomNav, navController);
@Override public boolean onSupportNavigateUp() { NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); return NavigationUI.navigateUp(navController, appBarConfiguration) || super.onSupportNavigateUp(); } @Override public boolean onOptionsItemSelected(MenuItem item) { NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); return NavigationUI.onNavDestinationSelected(item, navController) || super.onOptionsItemSelected(item); }
主要就是这两行代码便可,还要重写这两个方法来实现点击就能够切换了,首先咱们要获取到NavController 对象,这个对象获取有三种方式,这是其中一种了,而后调用NavigationUI这个类setup底部页卡和NavController 就关联起来了,这样就实现了切换功能,很重要的一点就是menu 的选项id必定要和nav的fragment的id保持一致,至此就完结了,咱们发现咱们省了不少切换逻辑代码,还有不少xml布局,这让咱们承载fragment的activity更加轻松简洁,大大缩短了逻辑代码,还有一个提示:页卡切换fragment咱们也能够在nav布局设置哦D)