Skip to content

Commit

Permalink
Merge 5dc240f into 40c6fdd
Browse files Browse the repository at this point in the history
  • Loading branch information
roughike committed Mar 29, 2017
2 parents 40c6fdd + 5dc240f commit cb258ac
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 16 deletions.
64 changes: 52 additions & 12 deletions bottom-bar/src/main/java/com/roughike/bottombar/BottomBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public class BottomBar extends LinearLayout implements View.OnClickListener, Vie
private boolean isComingFromRestoredState;
private boolean ignoreTabReselectionListener;

private Boolean pendingIsVisibleInShyMode;
private boolean shyHeightAlreadyCalculated;
private boolean navBarAccountedHeightCalculated;

Expand Down Expand Up @@ -394,6 +395,49 @@ private void resizeTabsToCorrectSizes(BottomBarTab[] tabsToAdd) {
}
}

/**
* Shows the BottomBar in shy mode, if it was hidden.
*
* If this BottomBar is a shy one (with bb_behavior set to "shy"), this
* will show it with a translate animation.
*
* If this BottomBar is NOT shy, or if it's already visible, this will be
* a no-op.
*/
public void showInShyMode() {
toggleIsVisibleInShyMode(true);
}

/**
* Hides the BottomBar in shy mode, if it was visible.
*
* If this BottomBar is a shy one (with bb_behavior set to "shy"), this
* will hide it with a translate animation.
*
* If this BottomBar is NOT shy, or if it's already hidden, this will be
* a no-op.
*/
public void hideInShyMode() {
toggleIsVisibleInShyMode(false);
}

private void toggleIsVisibleInShyMode(boolean visible) {
if (!isShy()) {
return;
}

if (shyHeightAlreadyCalculated) {
BottomNavigationBehavior<BottomBar> from = BottomNavigationBehavior.from(this);

if (from != null) {
boolean isHidden = !visible;
from.setHidden(this, isHidden);
}
} else {
pendingIsVisibleInShyMode = true;
}
}

/**
* Set a listener that gets fired when the selected {@link BottomBarTab} is about to change.
*
Expand Down Expand Up @@ -762,11 +806,19 @@ private void initializeShyBehavior() {

if (height != 0) {
updateShyHeight(height);
updatePendingShyVisibility();
shyHeightAlreadyCalculated = true;
}
}
}

private void updatePendingShyVisibility() {
if (pendingIsVisibleInShyMode != null) {
toggleIsVisibleInShyMode(pendingIsVisibleInShyMode);
pendingIsVisibleInShyMode = null;
}
}

private void updateShyHeight(int height) {
((CoordinatorLayout.LayoutParams) getLayoutParams())
.setBehavior(new BottomNavigationBehavior(height, 0, false));
Expand Down Expand Up @@ -1007,16 +1059,4 @@ private void onEnd() {
}
}).start();
}

/**
* Toggle translation of BottomBar to hidden and visible in a CoordinatorLayout.
*
* @param visible true resets translation to 0, false translates view to hidden
*/
private void toggleShyVisibility(boolean visible) {
BottomNavigationBehavior<BottomBar> from = BottomNavigationBehavior.from(this);
if (from != null) {
from.setHidden(this, visible);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,20 @@ void setHidden(@NonNull V view, boolean bottomLayoutHidden) {

static <V extends View> BottomNavigationBehavior<V> from(@NonNull V view) {
ViewGroup.LayoutParams params = view.getLayoutParams();

if (!(params instanceof CoordinatorLayout.LayoutParams)) {
throw new IllegalArgumentException("The view is not a child of CoordinatorLayout");
}

CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) params)
.getBehavior();
if (!(behavior instanceof BottomNavigationBehavior)) {
throw new IllegalArgumentException(
"The view is not associated with BottomNavigationBehavior");

if (behavior instanceof BottomNavigationBehavior) {
// noinspection unchecked
return (BottomNavigationBehavior<V>) behavior;
}
return (BottomNavigationBehavior<V>) behavior;

throw new IllegalArgumentException("The view is not associated with BottomNavigationBehavior");
}

private interface BottomNavigationWithSnackbar {
Expand Down

0 comments on commit cb258ac

Please sign in to comment.