Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed the shouldExpand behavior to set the layout at the time the tab is added #46

Merged
merged 1 commit into from
Dec 13, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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