Skip to content
Browse files

Navbar height calculation improvements.

  • Loading branch information...
1 parent 0cd9274 commit 68535e173e6b25d9154fb78468bb061764ecd508 @roughike committed
Showing with 31 additions and 21 deletions.
  1. +31 −21 bottom-bar/src/main/java/com/roughike/bottombar/BottomBar.java
View
52 bottom-bar/src/main/java/com/roughike/bottombar/BottomBar.java
@@ -64,6 +64,7 @@
private boolean mIgnoreTabletLayout;
private boolean mIsTabletMode;
private boolean mIsShy;
+ private boolean mShyHeightAlreadyCalculated;
private boolean mUseExtraOffset;
private ViewGroup mUserContentContainer;
@@ -189,24 +190,6 @@ public static BottomBar attachShy(CoordinatorLayout coordinatorLayout, Bundle sa
bottomBar.toughChildHood(ViewCompat.getFitsSystemWindows(coordinatorLayout));
bottomBar.onRestoreInstanceState(savedInstanceState);
- if (!coordinatorLayout.getContext().getResources().getBoolean(R.bool.bb_bottom_bar_is_tablet_mode)) {
- bottomBar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
- @SuppressWarnings("deprecation")
- @Override
- public void onGlobalLayout() {
- ((CoordinatorLayout.LayoutParams) bottomBar.getLayoutParams())
- .setBehavior(new BottomNavigationBehavior(bottomBar.getOuterContainer().getHeight(), 0));
- ViewTreeObserver obs = bottomBar.getViewTreeObserver();
-
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
- obs.removeOnGlobalLayoutListener(this);
- } else {
- obs.removeGlobalOnLayoutListener(this);
- }
- }
- });
- }
-
coordinatorLayout.addView(bottomBar);
return bottomBar;
}
@@ -591,7 +574,7 @@ private void initializeViews() {
mIsTabletMode = !mIgnoreTabletLayout &&
mContext.getResources().getBoolean(R.bool.bb_bottom_bar_is_tablet_mode);
- View rootView = View.inflate(mContext, mIsTabletMode?
+ View rootView = View.inflate(mContext, mIsTabletMode ?
R.layout.bb_bottom_bar_item_container_tablet : R.layout.bb_bottom_bar_item_container,
null);
mTabletRightBorder = rootView.findViewById(R.id.bb_tablet_right_border);
@@ -616,6 +599,27 @@ private void initializeViews() {
mUserContentContainer.addView(mPendingUserContentView, 0, params);
}
+ if (mIsShy && !mIsTabletMode) {
+ getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
+ @SuppressWarnings("deprecation")
+ @Override
+ public void onGlobalLayout() {
+ if (!mShyHeightAlreadyCalculated) {
+ ((CoordinatorLayout.LayoutParams) getLayoutParams())
+ .setBehavior(new BottomNavigationBehavior(getOuterContainer().getHeight(), 0));
+ }
+
+ ViewTreeObserver obs = getViewTreeObserver();
+
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
+ obs.removeOnGlobalLayoutListener(this);
+ } else {
+ obs.removeGlobalOnLayoutListener(this);
+ }
+ }
+ });
+ }
+
addView(rootView);
}
@@ -631,6 +635,10 @@ protected boolean isShy() {
return mIsShy;
}
+ protected void shyHeightAlreadyCalculated() {
+ mShyHeightAlreadyCalculated = true;
+ }
+
protected boolean useExtraOffset() {
return mUseExtraOffset;
}
@@ -717,7 +725,7 @@ private void updateItems(BottomBarItemBase[] bottomBarItems) {
if (mIsShiftingMode && !mIsTabletMode) {
layoutResource = R.layout.bb_bottom_bar_item_shifting;
} else {
- layoutResource = mIsTabletMode?
+ layoutResource = mIsTabletMode ?
R.layout.bb_bottom_bar_item_fixed_tablet : R.layout.bb_bottom_bar_item_fixed;
}
@@ -1100,11 +1108,13 @@ private static void navBarMagic(Activity activity, final BottomBar bottomBar) {
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
+ bottomBar.shyHeightAlreadyCalculated();
+
int newHeight = outerContainer.getHeight() + navBarHeightCopy;
outerContainer.getLayoutParams().height = newHeight;
if (bottomBar.isShy()) {
- int defaultOffset = bottomBar.useExtraOffset()? navBarHeightCopy : 0;
+ int defaultOffset = bottomBar.useExtraOffset() ? navBarHeightCopy : 0;
bottomBar.setTranslationY(defaultOffset);
((CoordinatorLayout.LayoutParams) bottomBar.getLayoutParams())
.setBehavior(new BottomNavigationBehavior(newHeight, defaultOffset));

0 comments on commit 68535e1

Please sign in to comment.
Something went wrong with that request. Please try again.