Skip to content

Commit

Permalink
Changed the shouldExpand behavior to set the layout at the time the t…
Browse files Browse the repository at this point in the history
…ab is added rather than in onMeasure to solve some problems observed on Android < 4.3
  • Loading branch information
Dmitry Apresian committed Nov 22, 2013
1 parent c195ccd commit 8a4795e
Showing 1 changed file with 9 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -204,8 +202,6 @@ public void notifyDataSetChanged() {

updateTabStyles();

checkedTabWidths = false;

getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

@SuppressWarnings("deprecation")
Expand All @@ -230,36 +226,32 @@ 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) {
pager.setCurrentItem(position);
}
});

tabsContainer.addView(tab);

tab.setPadding(tabPadding, 0, tabPadding, 0);
tabsContainer.addView(tab, position, shouldExpand ? expandedTabLayoutParams : defaultTabLayoutParams);
}

private void updateTabStyles() {
Expand All @@ -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) {

Expand All @@ -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) {
Expand Down

0 comments on commit 8a4795e

Please sign in to comment.