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
make main page tabs scrollable and hide when there is only a single tab #2871
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this needed feature! :-D
The layout width calculation can be improved, see below ;-)
private void resetMode() { | ||
if (getTabCount() < 2) { | ||
setVisibility(View.GONE); | ||
return; | ||
} else { | ||
setVisibility(View.VISIBLE); | ||
} | ||
|
||
if (getTabCount() == 0 || getTabAt(0).view == null) 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; | ||
for (int i = 0; i < getTabCount(); ++i) { | ||
if (getTabAt(i).view == null) return; | ||
actualWidth += ((View) getTabAt(i).view).getWidth(); | ||
if (actualWidth > layoutWidth) { | ||
setTabMode(TabLayout.MODE_SCROLLABLE); | ||
return; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add some comments to this function? For example explaining why getTabAt(0).view
could be null
and why you check for width twice (I assume you first do a fast check to improve performance?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getTabAt()
has a @Nullable
annotation. I'll make this function a little cleaner and account for possible null tabs differently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, it returns null
if index exceeds getTabCount()
.
There were no checks for whether Tab.view
is null in some Tab
's public methods modifying view
so I think I can assume that it's not null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a few comments throughout the class and simplified the calculation of tabs' width. I also fixed my mistake where width was calculated based only on minimum width requested by tab even if it's contents were wider.
app/src/main/java/org/schabi/newpipe/views/ScrollableTabLayout.java
Outdated
Show resolved
Hide resolved
Version |
ec48abe
to
aa69d4e
Compare
The changes look reasonable to me. @Stypox You began to review this. Can you please have a final look? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, the ScrollableTabLayout class is now ok! I only pointed out a little inconsistency :-)
app/src/main/java/org/schabi/newpipe/views/ScrollableTabLayout.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, all is good now :-D
fix bug where only minimum width requested by tab was counted even if actual content was wider
24415b7
to
3625a38
Compare
This PR makes tabs scrollable so they don't get squeezed if there are too many of them and hides tab selector if there is only a single tab. Adds and closes #1647
Created separate ScrollableTabLayout class to avoid cluttering MainFragment with switching between scrollable and fixed modes.