[Android] Hide/Show bottomNavigationView on Scroll

박준성·2021년 11월 12일
1

Android

목록 보기
2/3
post-thumbnail

UPDATE Just add one attribute to BottomNavigationView

AndroidX

<com.google.android.material.bottomnavigation.BottomNavigationView
 ....
 app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"/>

28.0.0또는higher version

XML은 아래 추가 작성 필요

BottomNavigationViewBehavior - Class 생성

public class BottomNavigationViewBehavior extends CoordinatorLayout.Behavior<BottomNavigationView> {

   private int height;

   @Override
   public boolean onLayoutChild(CoordinatorLayout parent, BottomNavigationView child, int layoutDirection) {
       height = child.getHeight();
       return super.onLayoutChild(parent, child, layoutDirection);
   }

   @Override
   public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
                                  BottomNavigationView child, @NonNull 
                                  View directTargetChild, @NonNull View target,
                                  int axes, int type)
   {
       return axes == ViewCompat.SCROLL_AXIS_VERTICAL;
   }

   @Override
   public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull BottomNavigationView child,
              @NonNull View target, int dxConsumed, int dyConsumed,
              int dxUnconsumed, int dyUnconsumed, 
               @ViewCompat.NestedScrollType int type)
   {
      if (dyConsumed > 0) {
          slideDown(child);
      } else if (dyConsumed < 0) {
          slideUp(child);
      }
   }

   private void slideUp(BottomNavigationView child) {
       child.clearAnimation();
       child.animate().translationY(0).setDuration(200);
   }

   private void slideDown(BottomNavigationView child) {
       child.clearAnimation();
       child.animate().translationY(height).setDuration(200);
   }
}
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.kliff.digitaldwarka.activity.MainActivity">

   <android.support.design.widget.CoordinatorLayout
       android:id="@+id/coordinator_layout"
       android:layout_width="match_parent"
       android:layout_height="match_parent">

       <android.support.design.widget.AppBarLayout
           android:id="@+id/myAppBar"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:descendantFocusability="beforeDescendants"
           android:focusableInTouchMode="true"
           android:theme="@style/AppTheme.AppBarOverlay"
           app:elevation="0dp">

           <android.support.v7.widget.Toolbar
               android:id="@+id/toolbar"
               android:layout_width="match_parent"
               android:layout_height="?attr/actionBarSize"
               android:background="?attr/colorPrimary"
               app:contentInsetStart="0dp"
               app:layout_scrollFlags="scroll|enterAlways"
               app:popupTheme="@style/AppTheme.PopupOverlay"/>
       </android.support.design.widget.AppBarLayout>

       <!---your RecyclerView/Fragment Container Layout-->
       <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
       

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottom_nav"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:itemBackground="@color/white"
            app:menu="@menu/bottom_nav_menu" />

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

     <!---NavigationView-->
</android.support.v4.widget.DrawerLayout>

BottomNavigationView 페이지에 내용 추가

mBottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) mBottomNavigationView.getLayoutParams();
    layoutParams.setBehavior(new BottomNavigationViewBehavior());

0개의 댓글