Skip to content

fix: lazy tab bar slow with or without animations #474

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

Merged
merged 1 commit into from
Feb 20, 2025
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 @@ -168,6 +168,7 @@ public void forceTransitionToPosition(int position){
return;
}
mTabStrip.onTabsViewPagerPageChanged(position, 0);
makeTabVisible(position, true);
}

/**
Expand Down Expand Up @@ -423,6 +424,54 @@ private void scrollToTab(int tabIndex, int positionOffset) {
}
}

private void makeTabVisible(int tabIndex) {
makeTabVisible(tabIndex, true);
}

private void makeTabVisible(int tabIndex, boolean smoothScroll) {
final int tabStripChildCount = mTabStrip.getChildCount();
if (tabStripChildCount == 0 || tabIndex < 0 || tabIndex >= tabStripChildCount) {
return;
}

View selectedChild = mTabStrip.getChildAt(tabIndex);
if (selectedChild == null) {
return;
}
int selectedLeft = selectedChild.getLeft();
int selectedRight = selectedChild.getRight();

int myWidth = getWidth();

// add padding to the sides so it's clear that we have more tabs to scroll to
if (tabIndex > 0) {
selectedLeft = selectedLeft - mTitleOffset;
}
if (tabIndex < tabStripChildCount - 1) {
selectedRight = selectedRight + mTitleOffset;
}

int scrollX = getScrollX();
if (selectedLeft < scrollX && selectedRight > myWidth + scrollX) {
// the tab is already visible and can't fit the screen, no need to scroll
return;
}

if (selectedLeft < scrollX) {
if (smoothScroll) {
smoothScrollTo(selectedLeft, 0);
} else {
scrollTo(selectedLeft, 0);
}
} else if (selectedRight > myWidth + scrollX) {
if (smoothScroll) {
smoothScrollTo(selectedRight - myWidth, 0);
} else {
scrollTo(selectedRight - myWidth, 0);
}
}
}

private class InternalViewPagerListener extends OnPageChangeCallback {
private int mScrollState;

Expand All @@ -434,7 +483,7 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse
}
mTabStrip.onTabsViewPagerPageChanged(position, positionOffset);
if (positionOffset == 0 && mScrollState == androidx.viewpager.widget.ViewPager.SCROLL_STATE_SETTLING) {
scrollToTab(position, 0);
makeTabVisible(position);
}
}

Expand Down