Search code examples
androidbehaviorbottomnavigationview

When scroll bottom navigation bar does not hide - BottomNavigationBehavior


i want to hide bottom navigation bar so i am using bottom navigation behavior it works when i had other code but in this it seems not working i tried but bottom navigation bar won't hide Here is the code

 package com.blipclap.creativegraphy.Helper;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;

import static java.lang.Float.parseFloat;

public class BottomNavigationBehaviour extends CoordinatorLayout.Behavior {

    public BottomNavigationBehaviour() {
    }

    public BottomNavigationBehaviour(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View 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 View child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type);
        child.setTranslationY(Math.max(0f,
                Math.min(Float.parseFloat(String.valueOf(child.getHeight())),child.getTranslationY()+dyConsumed)));
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        if (dependency instanceof Snackbar.SnackbarLayout)
            updateSnackbar(child,dependency);
        return super.layoutDependsOn(parent, child, dependency);
    }

    private void updateSnackbar(View child, View dependency) {
        if (dependency.getLayoutParams()instanceof CoordinatorLayout.LayoutParams) {
            CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) dependency.getLayoutParams();

            params.setAnchorId(child.getId());
            params.anchorGravity= Gravity.TOP;
            params.gravity=Gravity.TOP;
            dependency.setLayoutParams(params);

        }
    }
}

this is my layout content layout i tried changing relative layout but app unfortunately stops so i can't change it

<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.blipclap.creativegraphy.HomeActivity"
    tools:showIn="@layout/app_bar_home">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></android.support.design.widget.TabLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="android.support.v7.widget.LinearLayoutManager"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_above="@+id/navigation"
            android:layout_below="@+id/tabLayout"></android.support.v4.view.ViewPager>

        <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_gravity="bottom"
            app:layout_behavior=".Helper.BottomNavigationBehavior"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/colorPrimary"
            app:itemIconTint="@android:color/background_dark"
            app:itemTextColor="@android:color/background_dark"
            app:menu="@menu/bottom_navigation_menu"></android.support.design.widget.BottomNavigationView>
    </RelativeLayout>

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

this are my 3 fragments i am using when i scroll bottom navigation bar does not hide Category

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.blipclap.creativegraphy.Fragment.CategoryFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_category"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

</RelativeLayout>

daily popular

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.blipclap.creativegraphy.Fragment.CategoryFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_trending"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

</RelativeLayout>

recent

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.blipclap.creativegraphy.Fragment.CategoryFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_recent"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

</RelativeLayout>

this code is not working for me what should i do any solution

refrence

bottom navigation behavior reference 1

bottom navigation behavior reference 2

bottom navigation behavior reference 3

i followed the last one but its not working


Solution

  • There are two things:

    1) You should not override onNestedScroll, it should be onNestedPreScroll, delete onNestedScroll and replace it with that:

    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
        child.setTranslationY(Math.max(0f,
                Math.min(Float.parseFloat(String.valueOf(child.getHeight())),child.getTranslationY()+dy)));
    }
    

    2) In your XML file, you put

    app:layout_behavior=".Helper.BottomNavigationBehavior"
    

    And your class name is

    BottomNavigationBehaviour
    

    As you can see one is Behaviour, and the other is Behavior, normally it should throw a runtime error, and the app shouldn't be able to run, it may be a typo on your part, but I mentioned it just in case.

    But be aware that this code has a bug, if you try to scroll all the way down or up, the RecyclerView item will not be clickable for a couple of seconds, I have a similar bug. For now, my choice is to use animation to hide the BottomNavigationView as explained in this post.

    EDIT:

    It's possible that the behaviour is not applied because BottomNavigationView is not a direct child of CoordinatorLayout, so you can either delete the RelativeLayout entirely or take the BottomNavigationView out:

    <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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.blipclap.creativegraphy.HomeActivity"
        tools:showIn="@layout/app_bar_home">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white">
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"></android.support.design.widget.TabLayout>
    
            <android.support.v4.view.ViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layoutManager="android.support.v7.widget.LinearLayoutManager"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:layout_above="@+id/navigation"
                android:layout_below="@+id/tabLayout"></android.support.v4.view.ViewPager>
    
    
        </RelativeLayout>
    
    
        <android.support.design.widget.BottomNavigationView
                android:id="@+id/navigation"
                android:layout_gravity="bottom"
                app:layout_behavior=".Helper.BottomNavigationBehavior"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="@color/colorPrimary"
                app:itemIconTint="@android:color/background_dark"
                app:itemTextColor="@android:color/background_dark"
                app:menu="@menu/bottom_navigation_menu">
    
        </android.support.design.widget.BottomNavigationView>
    
    </android.support.design.widget.CoordinatorLayout>