Skip to content

Commit db798d3

Browse files
authored
Merge pull request #406 from nativescript-community/fix/tabstrip-animations
fix(tabs): fix weird behavior when disabling animations on tabstrip
2 parents 0fe8e40 + a747d58 commit db798d3

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

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

Lines changed: 13 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
@@ -418,7 +429,7 @@ private class InternalViewPagerListener extends OnPageChangeCallback {
418429
@Override
419430
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
420431
int tabStripChildCount = mTabStrip.getChildCount();
421-
if ((tabStripChildCount == 0) || (position < 0) || (position >= tabStripChildCount)) {
432+
if (mIgnoreScrollEventsUntilNextStateChange || (tabStripChildCount == 0) || (position < 0) || (position >= tabStripChildCount)) {
422433
return;
423434
}
424435
mTabStrip.onTabsViewPagerPageChanged(position, positionOffset);
@@ -429,6 +440,7 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse
429440

430441
@Override
431442
public void onPageScrollStateChanged (int state) {
443+
mIgnoreScrollEventsUntilNextStateChange = false;
432444
mScrollState = state;
433445
}
434446
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ export abstract class TabNavigation<T extends android.view.ViewGroup = any> exte
252252
protected abstract getTabBarItemView(index: number);
253253
protected abstract getTabBarItemTextView(index: number);
254254
protected abstract selectTabBar(oldIndex: number, newIndex: number);
255+
protected willSelectWithoutAnimation(index: number) {
256+
// noop
257+
}
255258

256259
private handleTabStripChanged(nativeView: org.nativescript.widgets.GridLayout, isNewView: boolean, newTabStrip: TabStrip) {
257260
if (this.mTabsBar) {
@@ -670,7 +673,14 @@ export abstract class TabNavigation<T extends android.view.ViewGroup = any> exte
670673
}
671674

672675
public onTabsBarSelectedPositionChange(position: number, prevPosition: number): void {
673-
this.mViewPager.setCurrentItem(position, this.animationEnabled);
676+
// prevent setting it to the same value
677+
// this is important when we're dragging and this is called with animationEnabled = false which will cause a jump
678+
if (position !== this.mViewPager.getCurrentItem()) {
679+
if (!this.animationEnabled) {
680+
this.willSelectWithoutAnimation(position);
681+
}
682+
this.mViewPager.setCurrentItem(position, this.animationEnabled);
683+
}
674684
const tabStripItems = this.tabStrip?.items;
675685

676686
if (position >= 0 && tabStripItems && tabStripItems[position]) {
@@ -703,6 +713,9 @@ export abstract class TabNavigation<T extends android.view.ViewGroup = any> exte
703713
[selectedIndexProperty.setNative](value: number) {
704714
const current = this.mViewPager.getCurrentItem();
705715
if (current !== value) {
716+
if (!this.animationEnabled) {
717+
this.willSelectWithoutAnimation(value);
718+
}
706719
this.mViewPager.setCurrentItem(value, this.animationEnabled);
707720
}
708721
}

src/tabs/index.android.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ export class Tabs extends TabNavigation<TabsBar> {
118118
}
119119
super.setTabStripItems(items);
120120
}
121+
protected override willSelectWithoutAnimation(index: number) {
122+
this.mTabsBar?.forceTransitionToPosition(index);
123+
}
121124

122125
public override onLoaded(): void {
123126
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)