底部导航栏的实现

底部导航栏的实现有多种方式:

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) {
              //具体代码
           }

       })

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值