From 68535e173e6b25d9154fb78468bb061764ecd508 Mon Sep 17 00:00:00 2001
From: Iiro <iiro.krankka@gmail.com>
Date: Mon, 21 Mar 2016 05:28:48 +0200
Subject: [PATCH] Navbar height calculation improvements.

---
 .../java/com/roughike/bottombar/BottomBar.java     | 52 +++++++++++++---------
 1 file changed, 31 insertions(+), 21 deletions(-)

diff --git a/bottom-bar/src/main/java/com/roughike/bottombar/BottomBar.java b/bottom-bar/src/main/java/com/roughike/bottombar/BottomBar.java
index 0ff24d9..e051566 100644
--- a/bottom-bar/src/main/java/com/roughike/bottombar/BottomBar.java
+++ b/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));
