simplify ScrollableTabLayout tabs width checking
This commit is contained in:
parent
f0f0c43b72
commit
b674cfec24
|
@ -25,19 +25,22 @@ public class ScrollableTabLayout extends TabLayout {
|
|||
|
||||
public ScrollableTabLayout(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
setTabMode(TabLayout.MODE_FIXED);
|
||||
}
|
||||
|
||||
public ScrollableTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
setTabMode(TabLayout.MODE_FIXED);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onAttachedToWindow() {
|
||||
super.onAttachedToWindow();
|
||||
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
||||
super.onLayout(changed, l, t, r, b);
|
||||
|
||||
setTabMode(TabLayout.MODE_FIXED);
|
||||
if (changed) {
|
||||
resetMode();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
||||
|
@ -68,21 +71,15 @@ public class ScrollableTabLayout extends TabLayout {
|
|||
setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
if (getTabCount() == 0 || getTabAt(0).view == null) return;
|
||||
int layoutWidth = getWidth();
|
||||
if (layoutWidth == 0) return;
|
||||
|
||||
setTabMode(TabLayout.MODE_FIXED);
|
||||
|
||||
int layoutWidth = getWidth();
|
||||
int minimumWidth = ((View) getTabAt(0).view).getMinimumWidth();
|
||||
if (minimumWidth * getTabCount() > layoutWidth) {
|
||||
setTabMode(TabLayout.MODE_SCROLLABLE);
|
||||
return;
|
||||
}
|
||||
|
||||
int actualWidth = 0;
|
||||
int tabsRequestedWidth = 0;
|
||||
for (int i = 0; i < getTabCount(); ++i) {
|
||||
if (getTabAt(i).view == null) return;
|
||||
actualWidth += ((View) getTabAt(i).view).getWidth();
|
||||
if (actualWidth > layoutWidth) {
|
||||
tabsRequestedWidth += ((View) getTabAt(i).view).getMinimumWidth();
|
||||
if (tabsRequestedWidth > layoutWidth) {
|
||||
setTabMode(TabLayout.MODE_SCROLLABLE);
|
||||
return;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue