Skip to content

Commit 469ea7e

Browse files
committed
fix(tabs): fix weird behavior when disabling animations on tabstrip
1 parent 52e2061 commit 469ea7e

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

packages/core-tabs/platforms/android/java/com/nativescript/material/core/TabsBar.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public interface TabColorizer {
7474

7575
private int mTitleOffset;
7676

77+
private boolean mIgnoreScrollEventsUntilNextStateChange = false;
78+
7779
private boolean mDistributeEvenly = true;
7880

7981
private TabItemSpec[] mTabItems;
@@ -159,6 +161,15 @@ public float getTabTextFontSize(){
159161
return mTabStrip.getTabTextFontSize();
160162
}
161163

164+
public void forceTransitionToPosition(int position){
165+
mIgnoreScrollEventsUntilNextStateChange = true;
166+
final int tabStripChildCount = mTabStrip.getChildCount();
167+
if ((tabStripChildCount == 0) || (position < 0) || (position >= tabStripChildCount)) {
168+
return;
169+
}
170+
mTabStrip.onTabsViewPagerPageChanged(position, 0);
171+
}
172+
162173
/**
163174
* Sets the associated view pager. Note that the assumption here is that the
164175
* pager content (number of tabs and tab titles) does not change after this
@@ -414,12 +425,18 @@ private class InternalViewPagerListener extends OnPageChangeCallback {
414425
@Override
415426
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
416427
int tabStripChildCount = mTabStrip.getChildCount();
417-
if ((tabStripChildCount == 0) || (position < 0) || (position >= tabStripChildCount)) {
428+
if (mIgnoreScrollEventsUntilNextStateChange || (tabStripChildCount == 0) || (position < 0) || (position >= tabStripChildCount)) {
418429
return;
419430
}
420431
mTabStrip.onTabsViewPagerPageChanged(position, positionOffset);
421432
}
422433

434+
@Override
435+
public void onPageScrollStateChanged(int state) {
436+
super.onPageScrollStateChanged(state);
437+
mIgnoreScrollEventsUntilNextStateChange = false;
438+
}
439+
423440
}
424441

425442
private class TabClickListener implements OnClickListener {

src/core-tabs/tab-navigation/index.android.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ export abstract class TabNavigation<T extends android.view.ViewGroup = any> exte
249249
protected abstract getTabBarItemView(index: number);
250250
protected abstract getTabBarItemTextView(index: number);
251251
protected abstract selectTabBar(oldIndex: number, newIndex: number);
252+
protected willSelectWithoutAnimation(index: number) {
253+
// noop
254+
}
252255

253256
private handleTabStripChanged(nativeView: org.nativescript.widgets.GridLayout, oldTabStrip: TabStrip, newTabStrip: TabStrip) {
254257
if (this.mTabsBar) {
@@ -663,7 +666,14 @@ export abstract class TabNavigation<T extends android.view.ViewGroup = any> exte
663666
}
664667

665668
public onTabsBarSelectedPositionChange(position: number, prevPosition: number): void {
666-
this.mViewPager.setCurrentItem(position, this.animationEnabled);
669+
// prevent setting it to the same value
670+
// this is important when we're dragging and this is called with animationEnabled = false which will cause a jump
671+
if (position !== this.mViewPager.getCurrentItem()) {
672+
if (!this.animationEnabled) {
673+
this.willSelectWithoutAnimation(position);
674+
}
675+
this.mViewPager.setCurrentItem(position, this.animationEnabled);
676+
}
667677
const tabStripItems = this.tabStrip?.items;
668678

669679
if (position >= 0 && tabStripItems && tabStripItems[position]) {
@@ -696,6 +706,9 @@ export abstract class TabNavigation<T extends android.view.ViewGroup = any> exte
696706
[selectedIndexProperty.setNative](value: number) {
697707
const current = this.mViewPager.getCurrentItem();
698708
if (current !== value) {
709+
if (!this.animationEnabled) {
710+
this.willSelectWithoutAnimation(value);
711+
}
699712
this.mViewPager.setCurrentItem(value, this.animationEnabled);
700713
}
701714
}

src/tabs/index.android.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ export class Tabs extends TabNavigation<TabsBar> {
105105
}
106106
super.setTabStripItems(items);
107107
}
108+
protected override willSelectWithoutAnimation(index: number) {
109+
this.mTabsBar?.forceTransitionToPosition(index);
110+
}
108111

109112
public override onLoaded(): void {
110113
super.onLoaded();

src/typings/extensions.android.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ declare namespace com {
2929
constructor(context: globalAndroid.content.Context, attrs: globalAndroid.util.AttributeSet);
3030
constructor(context: globalAndroid.content.Context, attrs: globalAndroid.util.AttributeSet, defStyle: number);
3131

32+
forceTransitionToPosition(position: number): void;
3233
setSelectedIndicatorColors(color: Array<number>): void;
3334
getSelectedIndicatorColors(): Array<number>;
3435
setTabTextColor(color: number): void;

0 commit comments

Comments
 (0)