Skip to content

Commit

Permalink
New API functionality base in #68 and #70 issues
.
Browse files Browse the repository at this point in the history
- Change order attr. Now the order is the same in attr.xml
and java class constructor.
- Better attr names, every attr related with tabs start with pstsTab
- Android attr only use for container
- No flipping the styles/appearance on selection/deselection tabs
- Remove custom attire `pstsTextColorSelected ` and
`pstsTextSelectedStyle `
- Callbacks for selection/deselection of tabs passing the tab view as
parameter when custom tabs are use.
  • Loading branch information
jpardogo committed May 20, 2015
1 parent bb734bd commit d8f1a1e
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 81 deletions.
24 changes: 10 additions & 14 deletions library/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,27 @@

<declare-styleable name="PagerSlidingTabStrip">
<attr name="pstsIndicatorColor" format="color" />
<attr name="pstsIndicatorHeight" format="dimension" />
<attr name="pstsUnderlineColor" format="color" />
<attr name="pstsUnderlineHeight" format="dimension" />
<attr name="pstsDividerColor" format="color" />
<attr name="pstsDividerWidth" format="dimension" />
<attr name="pstsIndicatorHeight" format="dimension" />
<attr name="pstsUnderlineHeight" format="dimension" />
<attr name="pstsDividerPadding" format="dimension" />
<attr name="pstsTabPaddingLeftRight" format="dimension" />
<attr name="pstsScrollOffset" format="dimension" />
<attr name="pstsTabBackground" format="reference" />
<attr name="pstsShouldExpand" format="boolean" />
<attr name="pstsTextAllCaps" format="boolean" />
<attr name="pstsPaddingMiddle" format="boolean" />
<attr name="pstsTextColorSelected" format="color" />
<attr name="pstsTextAlpha" format="integer" />
<attr name="pstsTextStyle">
<flag name="normal" value="0x0" />
<flag name="bold" value="0x1" />
<flag name="italic" value="0x2" />
</attr>
<attr name="pstsTextSelectedStyle">
<attr name="pstsTabPaddingLeftRight" format="dimension" />
<attr name="pstsTabBackground" format="reference" />
<attr name="pstsTabTextSize" format="dimension" />
<attr name="pstsTabTextColor" format="reference" />
<attr name="pstsTabTextStyle" format="reference">
<flag name="normal" value="0x0" />
<flag name="bold" value="0x1" />
<flag name="italic" value="0x2" />
</attr>
<attr name="pstsTextFontFamily" format="string" />
<attr name="pstsTabTextAllCaps" format="boolean" />
<attr name="pstsTabTextAlpha" format="integer" />
<attr name="pstsTabTextFontFamily" format="string" />
</declare-styleable>

</resources>
131 changes: 78 additions & 53 deletions library/src/com/astuetz/PagerSlidingTabStrip.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public class PagerSlidingTabStrip extends HorizontalScrollView {

public interface CustomTabProvider {
View getCustomTabView(ViewGroup parent, int position);

void tabSelected(View tab);

void tabUnselected(View tab);
}

public interface OnTabReselectedListener {
Expand All @@ -60,8 +64,6 @@ public interface OnTabReselectedListener {
// @formatter:off
private static final int[] ANDROID_ATTRS = new int[]{
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 @@ -72,11 +74,9 @@ public interface OnTabReselectedListener {

//These indexes must be related with the ATTR array above
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_INDEX = 3;
private static final int PADDING_LEFT_INDEX = 4;
private static final int PADDING_RIGHT_INDEX = 5;
private static final int PADDING_INDEX = 1;
private static final int PADDING_LEFT_INDEX = 2;
private static final int PADDING_RIGHT_INDEX = 3;

private LinearLayout.LayoutParams tabLayoutParams;

Expand Down Expand Up @@ -116,6 +116,7 @@ public interface OnTabReselectedListener {
private boolean shouldExpand = false;
private boolean textAllCaps = true;
private boolean isPaddingMiddle = false;
private boolean isCustomTabs;

private Typeface tabTypeface = null;
private String tabTypefaceName = "sans-serif";
Expand Down Expand Up @@ -144,6 +145,10 @@ public PagerSlidingTabStrip(Context context, AttributeSet attrs, int defStyle) {
tabsContainer.setOrientation(LinearLayout.HORIZONTAL);
addView(tabsContainer);

rectPaint = new Paint();
rectPaint.setAntiAlias(true);
rectPaint.setStyle(Style.FILL);

DisplayMetrics dm = getResources().getDisplayMetrics();
scrollOffset = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, scrollOffset, dm);
indicatorHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, indicatorHeight, dm);
Expand All @@ -153,11 +158,17 @@ public PagerSlidingTabStrip(Context context, AttributeSet attrs, int defStyle) {
dividerWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dividerWidth, dm);
tabTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, tabTextSize, dm);

// get system attrs (android:textSize and android:textColor)
dividerPaint = new Paint();
dividerPaint.setAntiAlias(true);
dividerPaint.setStrokeWidth(dividerWidth);

if (locale == null) {
locale = getResources().getConfiguration().locale;
}

// get system attrs for container
TypedArray a = context.obtainStyledAttributes(attrs, ANDROID_ATTRS);
int textPrimaryColor = a.getColor(TEXT_COLOR_PRIMARY, android.R.color.white);
tabTextSize = a.getDimensionPixelSize(TEXT_SIZE_INDEX, tabTextSize);
tabTextColor = a.hasValue(TEXT_COLOR_INDEX) ? a.getColorStateList(TEXT_COLOR_INDEX) : null;
underlineColor = textPrimaryColor;
dividerColor = textPrimaryColor;
indicatorColor = textPrimaryColor;
Expand All @@ -172,56 +183,50 @@ public PagerSlidingTabStrip(Context context, AttributeSet attrs, int defStyle) {
tabTypefaceStyle = Typeface.NORMAL;
}

// get custom attrs
// get custom attrs for tabs
a = context.obtainStyledAttributes(attrs, R.styleable.PagerSlidingTabStrip);
indicatorColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsIndicatorColor, indicatorColor);
indicatorHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsIndicatorHeight, indicatorHeight);
underlineColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsUnderlineColor, underlineColor);
underlineHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsUnderlineHeight, underlineHeight);
dividerColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsDividerColor, dividerColor);
dividerWidth = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsDividerWidth, dividerWidth);
indicatorHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsIndicatorHeight, indicatorHeight);
underlineHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsUnderlineHeight, underlineHeight);
dividerPadding = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsDividerPadding, dividerPadding);
tabPadding = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsTabPaddingLeftRight, tabPadding);
tabBackgroundResId = a.getResourceId(R.styleable.PagerSlidingTabStrip_pstsTabBackground, tabBackgroundResId);
shouldExpand = a.getBoolean(R.styleable.PagerSlidingTabStrip_pstsShouldExpand, shouldExpand);
scrollOffset = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsScrollOffset, scrollOffset);
textAllCaps = a.getBoolean(R.styleable.PagerSlidingTabStrip_pstsTextAllCaps, textAllCaps);
isPaddingMiddle = a.getBoolean(R.styleable.PagerSlidingTabStrip_pstsPaddingMiddle, isPaddingMiddle);
tabTypefaceStyle = a.getInt(R.styleable.PagerSlidingTabStrip_pstsTextStyle, tabTypefaceStyle);
String fontFamily = a.getString(R.styleable.PagerSlidingTabStrip_pstsTextFontFamily);
textAlpha = a.getInt(R.styleable.PagerSlidingTabStrip_pstsTextAlpha, textAlpha);
tabPadding = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsTabPaddingLeftRight, tabPadding);
tabBackgroundResId = a.getResourceId(R.styleable.PagerSlidingTabStrip_pstsTabBackground, tabBackgroundResId);
tabTextSize = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsTabTextSize, tabTextSize);
tabTextColor = a.hasValue(R.styleable.PagerSlidingTabStrip_pstsTabTextColor) ? a.getColorStateList(R.styleable.PagerSlidingTabStrip_pstsTabTextColor) : null;
tabTypefaceStyle = a.getInt(R.styleable.PagerSlidingTabStrip_pstsTabTextStyle, tabTypefaceStyle);
textAllCaps = a.getBoolean(R.styleable.PagerSlidingTabStrip_pstsTabTextAllCaps, textAllCaps);
textAlpha = a.getInt(R.styleable.PagerSlidingTabStrip_pstsTabTextAlpha, textAlpha);
String fontFamily = a.getString(R.styleable.PagerSlidingTabStrip_pstsTabTextFontFamily);
a.recycle();

if (fontFamily != null) {
tabTypefaceName = fontFamily;
}

//Tab text color selector
if (tabTextColor == null) {
tabTextColor = createColorStateList(textPrimaryColor, Color.argb(textAlpha,
Color.red(textPrimaryColor),
Color.green(textPrimaryColor),
Color.blue(textPrimaryColor)), textPrimaryColor);
tabTextColor = createColorStateList(
textPrimaryColor,
textPrimaryColor,
Color.argb(textAlpha,
Color.red(textPrimaryColor),
Color.green(textPrimaryColor),
Color.blue(textPrimaryColor)));
}

//Tab text typeface and style
if (fontFamily != null) {
tabTypefaceName = fontFamily;
}
tabTypeface = Typeface.create(tabTypefaceName, tabTypefaceStyle);

//Bottom padding for the tabs container parent view to show indicator and underline
setTabsContainerParentViewPaddings();

rectPaint = new Paint();
rectPaint.setAntiAlias(true);
rectPaint.setStyle(Style.FILL);

dividerPaint = new Paint();
dividerPaint.setAntiAlias(true);
dividerPaint.setStrokeWidth(dividerWidth);

tabLayoutParams = shouldExpand ? new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 1.0f)
: new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);

if (locale == null) {
locale = getResources().getConfiguration().locale;
}
}

private void setTabsContainerParentViewPaddings() {
Expand All @@ -235,6 +240,7 @@ public void setViewPager(ViewPager pager) {
throw new IllegalStateException("ViewPager does not have adapter instance.");
}

isCustomTabs = pager.getAdapter() instanceof CustomTabProvider;
pager.setOnPageChangeListener(pageListener);
pager.getAdapter().registerDataSetObserver(adapterObserver);
adapterObserver.setAttached(true);
Expand All @@ -247,7 +253,7 @@ public void notifyDataSetChanged() {
View tabView;
for (int i = 0; i < tabCount; i++) {

if (pager.getAdapter() instanceof CustomTabProvider) {
if (isCustomTabs) {
tabView = ((CustomTabProvider) pager.getAdapter()).getCustomTabView(this, i);
} else {
tabView = LayoutInflater.from(getContext()).inflate(R.layout.psts_tab, this, false);
Expand Down Expand Up @@ -517,19 +523,21 @@ private void updateSelection(int position) {

private void notSelected(View tab) {
if (tab != null) {
TextView title = (TextView) tab.findViewById(R.id.psts_tab_title);
if (title != null) {
title.setSelected(false);
TextView tab_title = (TextView) tab.findViewById(R.id.psts_tab_title);
if (tab_title != null) {
tab_title.setSelected(false);
}
if (isCustomTabs) ((CustomTabProvider) pager.getAdapter()).tabUnselected(tab);
}
}

private void selected(View tab) {
if (tab != null) {
TextView title = (TextView) tab.findViewById(R.id.psts_tab_title);
if (title != null) {
title.setSelected(true);
TextView tab_title = (TextView) tab.findViewById(R.id.psts_tab_title);
if (tab_title != null) {
tab_title.setSelected(true);
}
if (isCustomTabs) ((CustomTabProvider) pager.getAdapter()).tabSelected(tab);
}
}

Expand Down Expand Up @@ -768,13 +776,30 @@ public void setTextColor(ColorStateList colorStateList) {
updateTabStyles();
}

private ColorStateList createColorStateList(int textColor) {
return new ColorStateList(new int[][]{new int[]{}}, new int[]{textColor});
}

private ColorStateList createColorStateList(int state_selected, int state_focused, int state_default) {
//TODO
return null;
private ColorStateList createColorStateList(int color_state_default) {
return new ColorStateList(
new int[][]{
new int[]{} //default
},
new int[]{
color_state_default //default
}
);
}

private ColorStateList createColorStateList(int color_state_pressed, int color_state_selected, int color_state_default) {
return new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_pressed}, //pressed
new int[]{android.R.attr.state_selected}, // enabled
new int[]{} //default
},
new int[]{
color_state_pressed,
color_state_selected,
color_state_default
}
);
}

public void setTypeface(Typeface typeface, int style) {
Expand Down
6 changes: 0 additions & 6 deletions sample/res/drawable/selector_text_tabs.xml

This file was deleted.

4 changes: 1 addition & 3 deletions sample/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
Expand All @@ -11,8 +10,7 @@
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:pstsPaddingMiddle="true" />
android:layout_height="?attr/actionBarSize" />

<android.support.v4.view.ViewPager
android:id="@+id/pager"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -18,7 +17,6 @@
import android.widget.TextView;

import com.astuetz.PagerSlidingTabStrip;
import com.balysv.materialripple.MaterialRippleLayout;

import static com.astuetz.PagerSlidingTabStrip.CustomTabProvider;

Expand Down Expand Up @@ -125,9 +123,19 @@ public boolean isViewFromObject(View v, Object o) {

@Override
public View getCustomTabView(ViewGroup parent, int position) {
MaterialRippleLayout materialRippleLayout = (MaterialRippleLayout) LayoutInflater.from(mContext).inflate(R.layout.custom_tab, parent, false);
((ImageView)materialRippleLayout.findViewById(R.id.image)).setImageResource(ICONS[position]);
return materialRippleLayout;
View tab = LayoutInflater.from(mContext).inflate(R.layout.custom_tab, parent, false);
((ImageView) tab.findViewById(R.id.image)).setImageResource(ICONS[position]);
return tab;
}

@Override
public void tabSelected(View tab) {

}

@Override
public void tabUnselected(View tab) {

}
}
}

0 comments on commit d8f1a1e

Please sign in to comment.