From 9f733e95bbc2cb678850f6176810206383154d5f Mon Sep 17 00:00:00 2001 From: Ataul Munim Date: Fri, 20 Feb 2015 15:36:34 +0000 Subject: [PATCH] Add parsing of independent padding attributes - Allows paddingLeft and paddingRight to be set separately - For existing apps that _only_ specify left or right, they must add the other property for no change to be effected - For existing apps that specify the same padding for left and right, they needn't do anything to maintain behaviour - For existing apps that specify different values for left and right padding, they must increase the smaller padding to match the higher one to maintain behaviour - For existing apps that use `app:pstsPaddingMiddle="true"` no change is required to maintain behaviour (this seems to override the padding values anyway) --- .../src/com/astuetz/PagerSlidingTabStrip.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/library/src/com/astuetz/PagerSlidingTabStrip.java b/library/src/com/astuetz/PagerSlidingTabStrip.java index 86948299..7f4a8e0b 100644 --- a/library/src/com/astuetz/PagerSlidingTabStrip.java +++ b/library/src/com/astuetz/PagerSlidingTabStrip.java @@ -65,6 +65,7 @@ public interface OnTabReselectedListener { android.R.attr.textColorPrimary, android.R.attr.textSize, android.R.attr.textColor, + android.R.attr.padding, android.R.attr.paddingLeft, android.R.attr.paddingRight, }; @@ -76,8 +77,9 @@ public interface OnTabReselectedListener { private static final int TEXT_COLOR_PRIMARY = 0; private static final int TEXT_SIZE_INDEX = 1; private static final int TEXT_COLOR_INDEX = 2; - private static final int PADDING_LEFT_INDEX = 3; - private static final int PADDING_RIGHT_INDEX = 4; + private static final int PADDING_INDEX = 3; + private static final int PADDING_LEFT_INDEX = 4; + private static final int PADDING_RIGHT_INDEX = 5; private LinearLayout.LayoutParams defaultTabLayoutParams; private LinearLayout.LayoutParams expandedTabLayoutParams; @@ -113,7 +115,8 @@ public interface OnTabReselectedListener { private float tabTextAlpha = HALF_TRANSP; private float tabTextSelectedAlpha = OPAQUE; - private int padding = 0; + private int paddingLeft = 0; + private int paddingRight = 0; private boolean shouldExpand = false; private boolean textAllCaps = true; @@ -170,13 +173,11 @@ public PagerSlidingTabStrip(Context context, AttributeSet attrs, int defStyle) { underlineColor = textPrimaryColor; dividerColor = textPrimaryColor; indicatorColor = textPrimaryColor; - int paddingLeft = a.getDimensionPixelSize(PADDING_LEFT_INDEX, padding); - int paddingRight = a.getDimensionPixelSize(PADDING_RIGHT_INDEX, padding); + int padding = a.getDimensionPixelSize(PADDING_INDEX, 0); + paddingLeft = padding > 0 ? padding : a.getDimensionPixelSize(PADDING_LEFT_INDEX, 0); + paddingRight = padding > 0 ? padding : a.getDimensionPixelSize(PADDING_RIGHT_INDEX, 0); a.recycle(); - //In case we have the padding they must be equal so we take the biggest - padding = Math.max(paddingLeft, paddingRight); - // get custom attrs a = context.obtainStyledAttributes(attrs, R.styleable.PagerSlidingTabStrip); indicatorColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsIndicatorColor, indicatorColor); @@ -370,7 +371,7 @@ private Pair getIndicatorCoordinates() { @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { - if (isPaddingMiddle || padding > 0) { + if (isPaddingMiddle || paddingLeft > 0 || paddingRight > 0) { //Make sure tabContainer is bigger than the HorizontalScrollView to be able to scroll tabsContainer.setMinimumWidth(getWidth()); //Clipping padding to false to see the tabs while we pass them swiping @@ -400,10 +401,10 @@ public void onGlobalLayout() { if (isPaddingMiddle) { int mHalfWidthFirstTab = view.getWidth() / 2; - padding = getWidth() / 2 - mHalfWidthFirstTab; + paddingLeft = paddingRight = getWidth() / 2 - mHalfWidthFirstTab; } - setPadding(padding, getPaddingTop(), padding, getPaddingBottom()); - if (scrollOffset == 0) scrollOffset = getWidth() / 2 - padding; + setPadding(paddingLeft, getPaddingTop(), paddingRight, getPaddingBottom()); + if (scrollOffset == 0) scrollOffset = getWidth() / 2 - paddingLeft; } }; @@ -418,10 +419,10 @@ protected void onDraw(Canvas canvas) { // draw indicator line rectPaint.setColor(indicatorColor); Pair lines = getIndicatorCoordinates(); - canvas.drawRect(lines.first + padding, height - indicatorHeight, lines.second + padding, height, rectPaint); + canvas.drawRect(lines.first + paddingLeft, height - indicatorHeight, lines.second + paddingLeft, height, rectPaint); // draw underline rectPaint.setColor(underlineColor); - canvas.drawRect(padding, height - underlineHeight, tabsContainer.getWidth() + padding, height, rectPaint); + canvas.drawRect(paddingLeft, height - underlineHeight, tabsContainer.getWidth() + paddingRight, height, rectPaint); // draw divider if (dividerWidth != 0) { dividerPaint.setStrokeWidth(dividerWidth);