Skip to content

Commit

Permalink
Add parsing of independent padding attributes
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
ataulm committed Feb 20, 2015
1 parent 6c56093 commit 9f733e9
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions library/src/com/astuetz/PagerSlidingTabStrip.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -370,7 +371,7 @@ private Pair<Float, Float> 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
Expand Down Expand Up @@ -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;
}
};

Expand All @@ -418,10 +419,10 @@ protected void onDraw(Canvas canvas) {
// draw indicator line
rectPaint.setColor(indicatorColor);
Pair<Float, Float> 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);
Expand Down

0 comments on commit 9f733e9

Please sign in to comment.