Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for any FloatingActionButton library #295

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.animation.LinearOutSlowInInterpolator;
Expand Down Expand Up @@ -1246,11 +1245,14 @@ public void setBehaviorTranslationEnabled(boolean behaviorTranslationEnabled) {

/**
* Manage the floating action button behavior with AHBottomNavigation
* Method could also be used with custom libraries for FAB such as Clans/FloatingActionButton.
* Factor added to adjust the Y based on the FAB used.
* TIP: Factor of 1.48f works well for Clans/FloatingActionButton
* @param fab Floating Action Button
*/
public void manageFloatingActionButtonBehavior(FloatingActionButton fab) {
public <T extends View> void manageFloatingActionButtonBehavior(T fab, float factor) {
if (fab.getParent() instanceof CoordinatorLayout) {
AHBottomNavigationFABBehavior fabBehavior = new AHBottomNavigationFABBehavior(navigationBarHeight);
AHBottomNavigationFABBehavior fabBehavior = new AHBottomNavigationFABBehavior(navigationBarHeight, factor);
((CoordinatorLayout.LayoutParams) fab.getLayoutParams())
.setBehavior(fabBehavior);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package com.aurelhubert.ahbottomnavigation;

import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.view.ViewGroup;

/**
*
*/
public class AHBottomNavigationFABBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> {
public class AHBottomNavigationFABBehavior<T extends View> extends CoordinatorLayout.Behavior<T> {

private int navigationBarHeight = 0;
private long lastSnackbarUpdate = 0;
private float factor = 1.0f;

public AHBottomNavigationFABBehavior(int navigationBarHeight) {
public AHBottomNavigationFABBehavior(int navigationBarHeight, float factor) {
this.navigationBarHeight = navigationBarHeight;
this.factor = (factor == 0.0f) ? 1.0f : factor;
}

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
public boolean layoutDependsOn(CoordinatorLayout parent, T child, View dependency) {
if (dependency != null && dependency instanceof Snackbar.SnackbarLayout) {
return true;
} else if (dependency != null && dependency instanceof AHBottomNavigation) {
Expand All @@ -29,29 +30,30 @@ public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton ch
}

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
public boolean onDependentViewChanged(CoordinatorLayout parent, T child, View dependency) {
updateFloatingActionButton(child, dependency);
return super.onDependentViewChanged(parent, child, dependency);
}

/**
* Update floating action button bottom margin
*/
private void updateFloatingActionButton(FloatingActionButton child, View dependency) {
if (child != null && dependency != null && dependency instanceof Snackbar.SnackbarLayout) {
private void updateFloatingActionButton(T child, View dependency) {
if(child == null || dependency == null) {
return;
}
if (dependency instanceof Snackbar.SnackbarLayout) {
lastSnackbarUpdate = System.currentTimeMillis();
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) child.getLayoutParams();
int fabDefaultBottomMargin = p.bottomMargin;
child.setY(dependency.getY() - fabDefaultBottomMargin);
} else if (child != null && dependency != null && dependency instanceof AHBottomNavigation) {
} else if (dependency instanceof AHBottomNavigation) {
// Hack to avoid moving the FAB when the AHBottomNavigation is moving (showing or hiding animation)
if (System.currentTimeMillis() - lastSnackbarUpdate < 30) {
return;
}
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) child.getLayoutParams();
int fabDefaultBottomMargin = p.bottomMargin;
child.setY(dependency.getY() - fabDefaultBottomMargin);
}

ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) child.getLayoutParams();
int fabDefaultBottomMargin = p.bottomMargin;
child.setY((dependency.getY()/factor) - fabDefaultBottomMargin);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ private void initUI() {
bottomNavigation.addItems(bottomNavigationItems);
}

bottomNavigation.manageFloatingActionButtonBehavior(floatingActionButton);
// For Android support library FAB factor of 1.0f is good
bottomNavigation.manageFloatingActionButtonBehavior(floatingActionButton, 1.0f);
bottomNavigation.setTranslucentNavigationEnabled(true);

bottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
Expand Down