Andoid实现顶部导航栏和底部导航滑动隐藏(BottomNavigationView,CoordinatorLayout)

首先这个效果需要引入design:compile 'com.android.support:design:26.+'

在布局文件中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:descendantFocusability="blocksDescendants"
    android:fitsSystemWindows="true">


    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="100dp">


            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsingToolBarLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#fff"
                app:expandedTitleMargin="16dp"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlwaysCollapsed">


                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">


                    <ImageView
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_centerVertical="true"
                        android:background="@mipmap/ic_launcher" />


                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:text="这不是标题" />
                </RelativeLayout>
                
            </android.support.design.widget.CollapsingToolbarLayout>


        </android.support.design.widget.AppBarLayout>


        <android.support.v4.widget.NestedScrollView
            android:id="@+id/nest_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="12112112211221212121" />


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="12112112211221212121" />


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="12112112211221212121" />


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="12112112211221212121" />


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="12112112211221212121" />


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="12112112211221212121" />


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="12112112211221212121" />


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:text="12112112211221212121" />
            </LinearLayout>


        </android.support.v4.widget.NestedScrollView>


    </android.support.design.widget.CoordinatorLayout>


    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        app:menu="@menu/navigation"
        android:background="@color/colorPrimary"
        android:layout_gravity="start"
        android:layout_alignParentBottom="true"
        android:fitsSystemWindows="true"
        android:layout_height="100dp" />


</RelativeLayout>

注意:在使用BottomNavigationView的时候会有个默认的动画位移效果,一般我们是不需要这个效果的,所以我们就要去掉;

BottomNavigationViewHelper.disableShiftMode(bottomNavigationView); 

public class BottomNavigationViewHelper {
    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);
        }
    }

}

接下来就要监听NestedScrollView的滚动状态从而来设置底部的导航的隐藏和显示

nestView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int
                    oldScrollX, int oldScrollY) {


                //上滑 并且 正在显示底部栏
                if (scrollY - oldScrollY > 0 && isBottomShow) {
                    isBottomShow = false;
                    //将Y属性变为底部栏高度  (相当于隐藏了)
                    navigation.animate().translationY(navigation.getHeight());
                } else if (scrollY - oldScrollY < 0 && !isBottomShow) {
                    isBottomShow = true;
                    navigation.animate().translationY(0);
                }

            }

        });

资源下载:https://download.csdn.net/download/m0_37177456/103777

效果图:

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android Studio中使用ViewPager和BottomNavigationView,你可以按照以下步骤进行操作: 1. 首先,在你的XML布局文件中添加一个ViewPager和一个BottomNavigationView。例如,你可以在LinearLayout中添加一个ViewPager和一个BottomNavigationView,如下所示: ``` <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"> <androidx.viewpager.widget.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottomNavigationView" android:layout_width="match_parent" android:layout_height="wrap_content" app:menu="@menu/bottom_nav_menu" /> </LinearLayout> ``` 2. 创建一个PagerAdapter来管理ViewPager的页面。你可以创建一个继承自FragmentPagerAdapter或FragmentStatePagerAdapter的类,并实现相应的方法。这些方法包括getItem()方法,用于返回ViewPager中的Fragment实例,以及getCount()方法,用于返回ViewPager中的页面数量。 3. 在MainActivity或相应的Activity中,将ViewPager与PagerAdapter关联起来,并设置ViewPager作为BottomNavigationView的监听器。在ViewPager的onPageSelected()方法中,你可以根据选中的页面来更新BottomNavigationView的选中项。 ``` ViewPager viewPager = findViewById(R.id.viewPager); viewPager.setAdapter(new YourPagerAdapter(getSupportFragmentManager())); BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView); bottomNavigationView.setOnNavigationItemSelectedListener(item -> { switch (item.getItemId()) { case R.id.menu_item1: viewPager.setCurrentItem(0); return true; case R.id.menu_item2: viewPager.setCurrentItem(1); return true; case R.id.menu_item3: viewPager.setCurrentItem(2); return true; } return false; }); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position) { case 0: bottomNavigationView.setSelectedItemId(R.id.menu_item1); break; case 1: bottomNavigationView.setSelectedItemId(R.id.menu_item2); break; case 2: bottomNavigationView.setSelectedItemId(R.id.menu_item3); break; } } @Override public void onPageScrollStateChanged(int state) { } }); ``` 4. 创建相应的Fragment用于显示ViewPager中的页面。你可以创建继承自Fragment的类,并在PagerAdapter的getItem()方法中返回这些Fragment的实例。每个Fragment将显示不同的内容。 通过按照以上步骤,在Android Studio中使用ViewPager和BottomNavigationView实现底部导航栏同步切换操作。记得在布局文件中添加菜单文件(即bottom_nav_menu.xml),并在代码中设置相应的选中项和页面切换逻辑。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值