simplify ScrollableTabLayout tabs width checking

This commit is contained in:
Paweł Matuszewski 2019-12-16 00:11:54 +01:00 committed by TobiGr
parent f0f0c43b72
commit b674cfec24
1 changed files with 13 additions and 16 deletions

View File

@ -25,18 +25,21 @@ public class ScrollableTabLayout extends TabLayout {
public ScrollableTabLayout(Context context, AttributeSet attrs) { public ScrollableTabLayout(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
setTabMode(TabLayout.MODE_FIXED);
} }
public ScrollableTabLayout(Context context, AttributeSet attrs, int defStyleAttr) { public ScrollableTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); super(context, attrs, defStyleAttr);
setTabMode(TabLayout.MODE_FIXED);
} }
@Override @Override
protected void onAttachedToWindow() { protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onAttachedToWindow(); super.onLayout(changed, l, t, r, b);
setTabMode(TabLayout.MODE_FIXED); if (changed) {
resetMode(); resetMode();
}
} }
@Override @Override
@ -68,21 +71,15 @@ public class ScrollableTabLayout extends TabLayout {
setVisibility(View.VISIBLE); setVisibility(View.VISIBLE);
} }
if (getTabCount() == 0 || getTabAt(0).view == null) return; int layoutWidth = getWidth();
if (layoutWidth == 0) return;
setTabMode(TabLayout.MODE_FIXED); setTabMode(TabLayout.MODE_FIXED);
int layoutWidth = getWidth(); int tabsRequestedWidth = 0;
int minimumWidth = ((View) getTabAt(0).view).getMinimumWidth();
if (minimumWidth * getTabCount() > layoutWidth) {
setTabMode(TabLayout.MODE_SCROLLABLE);
return;
}
int actualWidth = 0;
for (int i = 0; i < getTabCount(); ++i) { for (int i = 0; i < getTabCount(); ++i) {
if (getTabAt(i).view == null) return; tabsRequestedWidth += ((View) getTabAt(i).view).getMinimumWidth();
actualWidth += ((View) getTabAt(i).view).getWidth(); if (tabsRequestedWidth > layoutWidth) {
if (actualWidth > layoutWidth) {
setTabMode(TabLayout.MODE_SCROLLABLE); setTabMode(TabLayout.MODE_SCROLLABLE);
return; return;
} }