From b674cfec241acfa6006c3b13fb419ff23dbc2a70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Matuszewski?= Date: Mon, 16 Dec 2019 00:11:54 +0100 Subject: [PATCH] simplify ScrollableTabLayout tabs width checking --- .../newpipe/views/ScrollableTabLayout.java | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/views/ScrollableTabLayout.java b/app/src/main/java/org/schabi/newpipe/views/ScrollableTabLayout.java index ffbb804af..40c021cec 100644 --- a/app/src/main/java/org/schabi/newpipe/views/ScrollableTabLayout.java +++ b/app/src/main/java/org/schabi/newpipe/views/ScrollableTabLayout.java @@ -25,18 +25,21 @@ 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); - resetMode(); + if (changed) { + resetMode(); + } } @Override @@ -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; }