2

I want to implement a hide on scroll BottomNavigationView. Googled around for solutions as there doesn't seem to exist one provided by android framework, that would work out of the box and my current setup is following:

<android.support.design.widget.CoordinatorLayout 
    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"
    tools:context=".MainActivity">

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="auto_fit"
        android:gravity="center"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:menu="@menu/menu_main">
    </android.support.design.widget.BottomNavigationView>
</android.support.design.widget.CoordinatorLayout>

MainActivity:

CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomNavigation.getLayoutParams();
layoutParams.setBehavior(new BottomNavigationViewBehavior());

BottomNavigationViewBehavior:

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);
    }
}

The onLayoutChild is called everytime, the parent is CoordinatorLayout and child is BottomNavigationView. But the onStartNestedScroll and onNestedScroll is never called, so the bottom navigation view don't get hidden on scroll. Tried switching the layouts around - fragment, no fragment, tried setting the bahavior through xml layout, nothing worked. What am I missing here?

1
  • Were you able to solve this? I'm facing the same issue :( Commented Feb 17, 2021 at 16:41

1 Answer 1

-1

Your xml should look something like this:

<android.support.design.widget.CoordinatorLayout
    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"
    tools:context=".MainActivity">

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="auto_fit"
        android:gravity="center"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:nestedScrollingEnabled="true"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:menu="@menu/menu_main"
        app:layout_behavior="BottomNavigationViewBehavior ">
    </android.support.design.widget.BottomNavigationView>
</android.support.design.widget.CoordinatorLayout>

And ditch this part:

    CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomNavigation.getLayoutParams();
    layoutParams.setBehavior(new BottomNavigationViewBehavior());

You have a similar example here

P.S. You should also change the GridView and use a RecyclerView with GridLayoutManager instead like you can see here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.