给Instagram项目增加BottomNavigationVi
在模仿Instagram项目(一)中添加了home主页的所有布局,top为tabs用于切换三个Fragment:Camera, Home和Message。
middle为viewPager,用adapter装在三个Fragments页面,bottom为bottomNavigationView用于切换menu,共含有五个item:home,search,circle,alert和android。home为跳转到主页面,search是搜索页面,circle是添加图片或视频,alert是关注,喜欢等功能,android是查看登陆账户的profile。
布局搭建完毕,我需要让页面和用户实现交互功能,我们先添加bottomNavigationView代码,这里我们用底部导航栏控件bottomNavigationViewEx来提高这个控件的效率和便携性。
一:在搭建布局时,我已经在gradle中添加了bottomNavigationViewEx依赖,要使用这个控件需要在两个地方添加:
1. build.gradle(Project: your_project_name)

2. build.gralde(Module:app)

二:添加BottomNavigationViewHelper,用来实现BottomNavigationViewEx控件功能
setBottomNavigationView用来控制控件图标的动画等功能,我们将所有动画效果和位移效果设置为false

enableNavigation用来实现当用户点击menu的不同item通过判断item名称,然后用startActivity方法跳转

三:HomeActivity.java中添加BottomNavigationViewEx设置
1. 当应用接收activity_home.xml布局后,添加自定义setupBottomNavigationView()方法

2. 通过findViewById关联bottomNavigationViewEx控件
调用BottomNavigationViewHelper.class中的setBottomNavigationVIew方法
调用BottomNavigationViewHelper.class中的enableNavigation方法
添加方法用来实现当用户点击BottomNavigationVIewEx底部导航栏时,跳转到不同的页面

我们看下BottomNavigationView的布局效果
