From e090462d04669e5b1d0a320e9ee48344f9da6a67 Mon Sep 17 00:00:00 2001 From: albul Date: Thu, 2 Nov 2017 12:27:03 +0200 Subject: [PATCH] Fixed: RequestLayout() improperly called #371, #518, #386 (cherry picked from commit e00149d) --- .../com/roughike/bottombar/BottomBar.java | 77 +++++++------------ .../bottombar/BottomBarTabContainer.java | 32 ++++++++ .../bb_bottom_bar_item_container.xml | 2 +- .../layout/bb_bottom_bar_item_container.xml | 2 +- .../bb_bottom_bar_item_container_tablet.xml | 2 +- .../layout/bb_bottom_bar_item_shifting.xml | 1 + 6 files changed, 64 insertions(+), 52 deletions(-) create mode 100644 bottom-bar/src/main/java/com/roughike/bottombar/BottomBarTabContainer.java 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 350cc316..105cb54a 100644 --- a/bottom-bar/src/main/java/com/roughike/bottombar/BottomBar.java +++ b/bottom-bar/src/main/java/com/roughike/bottombar/BottomBar.java @@ -31,7 +31,6 @@ import android.view.ViewOutlineProvider; import android.view.ViewParent; import android.widget.LinearLayout; -import android.widget.TextView; import android.widget.Toast; import java.util.List; @@ -65,7 +64,6 @@ public class BottomBar extends LinearLayout implements View.OnClickListener, Vie private BatchTabPropertyApplier batchPropertyApplier; private int primaryColor; private int screenWidth; - private int tenDp; private int maxFixedItemWidth; // XML Attributes @@ -87,7 +85,7 @@ public class BottomBar extends LinearLayout implements View.OnClickListener, Vie private View backgroundOverlay; private ViewGroup outerContainer; - private ViewGroup tabContainer; + private BottomBarTabContainer tabContainer; private int defaultBackgroundColor = Color.WHITE; private int currentBackgroundColor; @@ -185,7 +183,6 @@ private void init21(Context context) { private void populateAttributes(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { primaryColor = MiscUtils.getColor(getContext(), R.attr.colorPrimary); screenWidth = MiscUtils.getScreenWidth(getContext()); - tenDp = MiscUtils.dpToPixel(getContext(), 10); maxFixedItemWidth = MiscUtils.dpToPixel(getContext(), 168); TypedArray ta = context.getTheme() @@ -266,7 +263,7 @@ private void initializeViews() { backgroundOverlay = rootView.findViewById(R.id.bb_bottom_bar_background_overlay); outerContainer = (ViewGroup) rootView.findViewById(R.id.bb_bottom_bar_outer_container); - tabContainer = (ViewGroup) rootView.findViewById(R.id.bb_bottom_bar_item_container); + tabContainer = (BottomBarTabContainer) rootView.findViewById(R.id.bb_bottom_bar_item_container); shadowView = findViewById(R.id.bb_bottom_bar_shadow); } @@ -394,28 +391,43 @@ private void resizeTabsToCorrectSizes(BottomBarTab[] tabsToAdd) { inActiveShiftingItemWidth = (int) (proposedItemWidth * 0.9); activeShiftingItemWidth = (int) (proposedItemWidth + (proposedItemWidth * ((tabsToAdd.length - 1) * 0.1))); - int height = Math.round(getContext().getResources() + int tabHeight = Math.round(getContext().getResources() .getDimension(R.dimen.bb_height)); - - for (BottomBarTab tabView : tabsToAdd) { - ViewGroup.LayoutParams params = tabView.getLayoutParams(); - params.height = height; + int tabWidth; + BottomBarTab tabView; + ViewGroup.LayoutParams params; + boolean isParamsChanged; + for (int i = 0; i < tabsToAdd.length; i++) { + tabView = tabsToAdd[i]; + params = tabView.getLayoutParams(); if (isShiftingMode()) { if (tabView.isActive()) { - params.width = activeShiftingItemWidth; + tabWidth = activeShiftingItemWidth; } else { - params.width = inActiveShiftingItemWidth; + tabWidth = inActiveShiftingItemWidth; } } else { - params.width = proposedItemWidth; + tabWidth = proposedItemWidth; } - if (tabView.getParent() == null) { - tabContainer.addView(tabView); + if (params.height != tabHeight || params.width != tabWidth) { + params.height = tabHeight; + params.width = tabWidth; + isParamsChanged = true; + } else { + isParamsChanged = false; } - tabView.setLayoutParams(params); + if (tabView.getParent() == null) { + if (ViewCompat.isInLayout(this)) { + tabContainer.addViewInLayout(tabView, i, params); + } else { + tabContainer.addView(tabView, i, params); + } + } else if (isParamsChanged) { + tabView.setLayoutParams(params); + } } } @@ -761,8 +773,6 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto resizeTabsToCorrectSizes(currentTabs); } - updateTitleBottomPadding(); - if (isShy()) { initializeShyBehavior(); } @@ -773,37 +783,6 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto } } - private void updateTitleBottomPadding() { - if (isIconsOnlyMode()) { - return; - } - - int tabCount = getTabCount(); - - if (tabContainer == null || tabCount == 0 || !isShiftingMode()) { - return; - } - - for (int i = 0; i < tabCount; i++) { - BottomBarTab tab = getTabAtPosition(i); - TextView title = tab.getTitleView(); - - if (title == null) { - continue; - } - - int baseline = title.getBaseline(); - int height = title.getHeight(); - int paddingInsideTitle = height - baseline; - int missingPadding = tenDp - paddingInsideTitle; - - if (missingPadding > 0) { - title.setPadding(title.getPaddingLeft(), title.getPaddingTop(), - title.getPaddingRight(), missingPadding + title.getPaddingBottom()); - } - } - } - private void initializeShyBehavior() { ViewParent parent = getParent(); diff --git a/bottom-bar/src/main/java/com/roughike/bottombar/BottomBarTabContainer.java b/bottom-bar/src/main/java/com/roughike/bottombar/BottomBarTabContainer.java new file mode 100644 index 00000000..dce96a08 --- /dev/null +++ b/bottom-bar/src/main/java/com/roughike/bottombar/BottomBarTabContainer.java @@ -0,0 +1,32 @@ +package com.roughike.bottombar; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.View; +import android.view.ViewGroup; +import android.widget.LinearLayout; + +public class BottomBarTabContainer extends LinearLayout { + + public BottomBarTabContainer(Context context) { + super(context); + } + + public BottomBarTabContainer(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public BottomBarTabContainer(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @SuppressWarnings("NewApi") + public BottomBarTabContainer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + @Override + public boolean addViewInLayout(View child, int index, ViewGroup.LayoutParams params) { + return super.addViewInLayout(child, index, params); + } +} diff --git a/bottom-bar/src/main/res/layout-v21/bb_bottom_bar_item_container.xml b/bottom-bar/src/main/res/layout-v21/bb_bottom_bar_item_container.xml index 1185bd5f..e4d01671 100644 --- a/bottom-bar/src/main/res/layout-v21/bb_bottom_bar_item_container.xml +++ b/bottom-bar/src/main/res/layout-v21/bb_bottom_bar_item_container.xml @@ -12,7 +12,7 @@ android:layout_height="match_parent" android:visibility="invisible" /> - - -