From 8a4795e91d718f990e2f1705a2c238db9bcbe163 Mon Sep 17 00:00:00 2001 From: Dmitry Apresian Date: Fri, 22 Nov 2013 08:56:22 -0800 Subject: [PATCH] Changed the shouldExpand behavior to set the layout at the time the tab is added rather than in onMeasure to solve some problems observed on Android < 4.3 --- .../extensions/PagerSlidingTabStrip.java | 58 +++---------------- 1 file changed, 9 insertions(+), 49 deletions(-) diff --git a/library/src/com/astuetz/viewpager/extensions/PagerSlidingTabStrip.java b/library/src/com/astuetz/viewpager/extensions/PagerSlidingTabStrip.java index 10adeeaa..1032f348 100644 --- a/library/src/com/astuetz/viewpager/extensions/PagerSlidingTabStrip.java +++ b/library/src/com/astuetz/viewpager/extensions/PagerSlidingTabStrip.java @@ -71,8 +71,6 @@ public interface IconTabProvider { private Paint rectPaint; private Paint dividerPaint; - private boolean checkedTabWidths = false; - private int indicatorColor = 0xFF666666; private int underlineColor = 0x1A000000; private int dividerColor = 0x1A000000; @@ -204,8 +202,6 @@ public void notifyDataSetChanged() { updateTabStyles(); - checkedTabWidths = false; - getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @SuppressWarnings("deprecation") @@ -230,27 +226,23 @@ private void addTextTab(final int position, String title) { TextView tab = new TextView(getContext()); tab.setText(title); - tab.setFocusable(true); tab.setGravity(Gravity.CENTER); tab.setSingleLine(); - tab.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View v) { - pager.setCurrentItem(position); - } - }); - - tabsContainer.addView(tab); - + addTab(position, tab); } private void addIconTab(final int position, int resId) { ImageButton tab = new ImageButton(getContext()); - tab.setFocusable(true); tab.setImageResource(resId); + addTab(position, tab); + + } + + private void addTab(final int position, View tab) { + tab.setFocusable(true); tab.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { @@ -258,8 +250,8 @@ public void onClick(View v) { } }); - tabsContainer.addView(tab); - + tab.setPadding(tabPadding, 0, tabPadding, 0); + tabsContainer.addView(tab, position, shouldExpand ? expandedTabLayoutParams : defaultTabLayoutParams); } private void updateTabStyles() { @@ -268,13 +260,7 @@ private void updateTabStyles() { View v = tabsContainer.getChildAt(i); - v.setLayoutParams(defaultTabLayoutParams); v.setBackgroundResource(tabBackgroundResId); - if (shouldExpand) { - v.setPadding(0, 0, 0, 0); - } else { - v.setPadding(tabPadding, 0, tabPadding, 0); - } if (v instanceof TextView) { @@ -297,32 +283,6 @@ private void updateTabStyles() { } - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - super.onMeasure(widthMeasureSpec, heightMeasureSpec); - - if (!shouldExpand || MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.UNSPECIFIED) { - return; - } - - int myWidth = getMeasuredWidth(); - int childWidth = 0; - for (int i = 0; i < tabCount; i++) { - childWidth += tabsContainer.getChildAt(i).getMeasuredWidth(); - } - - if (!checkedTabWidths && childWidth > 0 && myWidth > 0) { - - if (childWidth <= myWidth) { - for (int i = 0; i < tabCount; i++) { - tabsContainer.getChildAt(i).setLayoutParams(expandedTabLayoutParams); - } - } - - checkedTabWidths = true; - } - } - private void scrollToChild(int position, int offset) { if (tabCount == 0) {