底部导航栏的实现有多种方式:
1、使用TextView
2、使用RadioGroup +RadioButton 实现
3、BottomNavigationView 实现
4、BottomNavigationBar 实现
BottomNavigationView 实现:(最多可以设置5个item,多于5个会导致app crash)
xml布局:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation_view"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:menu="@menu/bottom_menu"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
bottom_menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_first"
android:icon="@drawable/top_pager_select"
android:title="首页"/>
<item
android:id="@+id/bottom_second"
android:icon="@mipmap/ic_launcher"
android:title="发现"/>
<item
android:id="@+id/bottom_third"
android:icon="@drawable/top_pager_unselect"
android:title="更多"/>
<item
android:id="@+id/bottom_forth"
android:icon="@mipmap/ic_launcher"
android:title="我的"/>
</menu>
这样写会出现问题,而且选择item的时候会有一个动画效果,如果设计人员对此没意见的话就OVER了。
如何让关闭动画并使图片文字全部显示?官方没有给我们对应的接口,需要反射来获取对象并设置属性:
public class BottomNavigationHelp {
@SuppressLint("RestrictedApi")
public static void disableShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
try {
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
//noinspection RestrictedApi
item.setShiftingMode(false);
// set once again checked value, so view will be updated
//noinspection RestrictedApi
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
}
再设置BottomNavigationView:
BottomNavigationHelp.disableShiftMode(bottom_navigation_view)
最后对BottomNavigationView的事件进行监听就可以实现切换效果:
bottom_navigation_view!!.setOnNavigationItemSelectedListener {item ->
when(item.itemId){
R.id.menu_first -> view_pager!!.currentItem =0
R.id.bottom_second -> view_pager!!.currentItem =1
R.id.bottom_third -> view_pager!!.currentItem =2
R.id.bottom_forth -> view_pager!!.currentItem = 3
}
true
}
BottomNavigationBar 的使用:xml布局:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.ashokvarma.bottomnavigation.BottomNavigationBar
android:id="@+id/bottom_bar_fragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<FrameLayout
android:id="@+id/frame_layout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bottom_bar_fragment"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
代码调用:(kotlin)
bottom_bar_fragment!!.addItem(BottomNavigationItem(R.drawable.top_pager_unselect, "图片")).setActiveColor("#00a0ff")
.addItem(BottomNavigationItem(R.drawable.top_pager_unselect, "视频")).setActiveColor("#00a0ff")
.addItem(BottomNavigationItem(R.drawable.top_pager_unselect, "高清大图")).setActiveColor("#00a0ff")
.addItem(BottomNavigationItem(R.drawable.top_pager_unselect, "超清视频")).setActiveColor("#00a0ff")
.initialise()
bottom_bar_fragment.setFirstSelectedPosition(0)
会出现问题,解决方案:在初始化bottom_bar_fragment之前调用:
bottom_bar_fragment.setMode(MODE_FIXED);
同样,交互也是通过BottomNavigationBar的回调来实现的:
bottom_bar!!.setTabSelectedListener(object :BottomNavigationBar.OnTabSelectedListener{
override fun onTabReselected(position: Int) {
}
override fun onTabUnselected(position: Int) {
}
override fun onTabSelected(position: Int) {
//具体代码
}
})