Skip to content

Instantly share code, notes, and snippets.

@jesselima
Created October 4, 2018 12:33
Show Gist options
  • Save jesselima/ccb3a44b0d0a05b4947605828beb18f7 to your computer and use it in GitHub Desktop.
Save jesselima/ccb3a44b0d0a05b4947605828beb18f7 to your computer and use it in GitHub Desktop.
package com.udacity.popularmovies.utils;
import android.content.Context;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
/**
* This class controls the show/hide behavior of BottonNavigation on Scroll.
* Created by Ravi Tamada on 21/12/17.
* https://www.androidhive.info/2017/12/android-working-with-bottom-navigation/
*/
@SuppressWarnings({"ALL", "NullableProblems"})
public class BottomNavigationBehavior extends CoordinatorLayout.Behavior<BottomNavigationView> {
public BottomNavigationBehavior() {
super();
}
public BottomNavigationBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, BottomNavigationView child, View dependency) {
return dependency instanceof FrameLayout;
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
}
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View target, int dx, int dy, int[] consumed) {
if (dy < 0) {
showBottomNavigationView(child);
} else if (dy > 0) {
hideBottomNavigationView(child);
}
}
private void hideBottomNavigationView(BottomNavigationView view) {
view.animate().translationY(view.getHeight());
}
private void showBottomNavigationView(BottomNavigationView view) {
view.animate().translationY(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment