feat(tabs): add animationEnabled property (#8704)

This commit is contained in:
Igor Randjelovic
2020-07-11 16:58:50 +02:00
committed by GitHub
parent 9834c4f887
commit 36aa181185
4 changed files with 33 additions and 20 deletions

View File

@ -43,4 +43,7 @@ tabsPositionProperty.register(TabsBase);
export type IOSTabBarItemsAlignment = "leading" | "justified" | "center" | "centerSelected"; export type IOSTabBarItemsAlignment = "leading" | "justified" | "center" | "centerSelected";
export const iOSTabBarItemsAlignmentProperty = new Property<TabsBase, IOSTabBarItemsAlignment>({ name: "iOSTabBarItemsAlignment", defaultValue: "justified" }); export const iOSTabBarItemsAlignmentProperty = new Property<TabsBase, IOSTabBarItemsAlignment>({ name: "iOSTabBarItemsAlignment", defaultValue: "justified" });
iOSTabBarItemsAlignmentProperty.register(TabsBase); iOSTabBarItemsAlignmentProperty.register(TabsBase);
export const animationEnabledProperty = new Property<TabsBase, boolean>({ name: "animationEnabled", defaultValue: true, valueConverter: booleanConverter });
animationEnabledProperty.register(TabsBase);

View File

@ -15,7 +15,7 @@ import {
getIconSpecSize, itemsProperty, selectedIndexProperty, tabStripProperty getIconSpecSize, itemsProperty, selectedIndexProperty, tabStripProperty
} from "../tab-navigation-base/tab-navigation-base"; } from "../tab-navigation-base/tab-navigation-base";
import { getTransformedText } from "../text-base"; import { getTransformedText } from "../text-base";
import { offscreenTabLimitProperty, swipeEnabledProperty, TabsBase } from "./tabs-common"; import { offscreenTabLimitProperty, swipeEnabledProperty, animationEnabledProperty, TabsBase } from "./tabs-common";
export * from "./tabs-common"; export * from "./tabs-common";
@ -378,6 +378,7 @@ export class Tabs extends TabsBase {
private _textTransform: TextTransform = "uppercase"; private _textTransform: TextTransform = "uppercase";
private _selectedItemColor: Color; private _selectedItemColor: Color;
private _unSelectedItemColor: Color; private _unSelectedItemColor: Color;
public animationEnabled: boolean;
constructor() { constructor() {
super(); super();
@ -946,14 +947,11 @@ export class Tabs extends TabsBase {
} }
[selectedIndexProperty.setNative](value: number) { [selectedIndexProperty.setNative](value: number) {
const smoothScroll = true;
// TODO // TODO
// if (traceEnabled()) { // if (traceEnabled()) {
// traceWrite("TabView this._viewPager.setCurrentItem(" + value + ", " + smoothScroll + ");", traceCategory); // traceWrite("TabView this._viewPager.setCurrentItem(" + value + ", " + smoothScroll + ");", traceCategory);
// } // }
this._viewPager.setCurrentItem(value, this.animationEnabled);
this._viewPager.setCurrentItem(value, smoothScroll);
} }
[itemsProperty.getDefault](): TabContentItem[] { [itemsProperty.getDefault](): TabContentItem[] {
@ -985,6 +983,10 @@ export class Tabs extends TabsBase {
[offscreenTabLimitProperty.setNative](value: number) { [offscreenTabLimitProperty.setNative](value: number) {
this._viewPager.setOffscreenPageLimit(value); this._viewPager.setOffscreenPageLimit(value);
} }
[animationEnabledProperty.setNative](value: number) {
(<any>this._viewPager).setAnimationEnabled(value);
}
} }
function tryCloneDrawable(value: android.graphics.drawable.Drawable, resources: android.content.res.Resources): android.graphics.drawable.Drawable { function tryCloneDrawable(value: android.graphics.drawable.Drawable, resources: android.content.res.Resources): android.graphics.drawable.Drawable {

View File

@ -13,9 +13,17 @@ import { ios as iosView, View } from "../core/view";
import { Frame } from "../frame"; import { Frame } from "../frame";
import { Font } from "../styling/font"; import { Font } from "../styling/font";
import { import {
getIconSpecSize, itemsProperty, selectedIndexProperty, tabStripProperty, getIconSpecSize,
itemsProperty,
selectedIndexProperty,
tabStripProperty,
} from "../tab-navigation-base/tab-navigation-base"; } from "../tab-navigation-base/tab-navigation-base";
import { swipeEnabledProperty, TabsBase, IOSTabBarItemsAlignment, iOSTabBarItemsAlignmentProperty } from "./tabs-common"; import {
IOSTabBarItemsAlignment,
iOSTabBarItemsAlignmentProperty,
swipeEnabledProperty,
TabsBase
} from "./tabs-common";
// TODO // TODO
// import { profile } from "../../profiling"; // import { profile } from "../../profiling";
@ -463,6 +471,7 @@ export class Tabs extends TabsBase {
public _defaultItemBackgroundColor: UIColor; public _defaultItemBackgroundColor: UIColor;
private _selectedItemColor: Color; private _selectedItemColor: Color;
private _unSelectedItemColor: Color; private _unSelectedItemColor: Color;
public animationEnabled: boolean;
constructor() { constructor() {
super(); super();
@ -1102,7 +1111,7 @@ export class Tabs extends TabsBase {
// do not make layout changes while the animation is in progress https://stackoverflow.com/a/47031524/613113 // do not make layout changes while the animation is in progress https://stackoverflow.com/a/47031524/613113
this.visitFrames(item, frame => frame._animationInProgress = true); this.visitFrames(item, frame => frame._animationInProgress = true);
this.viewController.setViewControllersDirectionAnimatedCompletion(controllers, navigationDirection, true, (finished: boolean) => { invokeOnRunLoop( () => this.viewController.setViewControllersDirectionAnimatedCompletion(controllers, navigationDirection, this.animationEnabled, (finished: boolean) => {
this.visitFrames(item, frame => frame._animationInProgress = false); this.visitFrames(item, frame => frame._animationInProgress = false);
if (finished) { if (finished) {
// HACK: UIPageViewController fix; see https://stackoverflow.com/a/17330606 // HACK: UIPageViewController fix; see https://stackoverflow.com/a/17330606
@ -1112,10 +1121,10 @@ export class Tabs extends TabsBase {
this._setCanBeLoaded(value); this._setCanBeLoaded(value);
this._loadUnloadTabItems(value); this._loadUnloadTabItems(value);
} }
}); }));
if (this.tabBarItems && this.tabBarItems.length && this.viewController && this.viewController.tabBar) { if (this.tabBarItems && this.tabBarItems.length && this.viewController && this.viewController.tabBar) {
this.viewController.tabBar.setSelectedItemAnimated(this.tabBarItems[value], true); this.viewController.tabBar.setSelectedItemAnimated(this.tabBarItems[value], this.animationEnabled);
} }
// TODO: // TODO:
// (<any>this._ios)._willSelectViewController = this._ios.viewControllers[value]; // (<any>this._ios)._willSelectViewController = this._ios.viewControllers[value];
@ -1140,7 +1149,6 @@ export class Tabs extends TabsBase {
[tabStripProperty.getDefault](): TabStrip { [tabStripProperty.getDefault](): TabStrip {
return null; return null;
} }
[tabStripProperty.setNative](value: TabStrip) { [tabStripProperty.setNative](value: TabStrip) {
this.setViewControllers(this.items); this.setViewControllers(this.items);
selectedIndexProperty.coerce(this); selectedIndexProperty.coerce(this);
@ -1149,7 +1157,6 @@ export class Tabs extends TabsBase {
[swipeEnabledProperty.getDefault](): boolean { [swipeEnabledProperty.getDefault](): boolean {
return true; return true;
} }
[swipeEnabledProperty.setNative](value: boolean) { [swipeEnabledProperty.setNative](value: boolean) {
if (this.viewController && this.viewController.scrollView) { if (this.viewController && this.viewController.scrollView) {
this.viewController.scrollView.scrollEnabled = value; this.viewController.scrollView.scrollEnabled = value;
@ -1165,7 +1172,6 @@ export class Tabs extends TabsBase {
return <any>(alignment.charAt(0).toLowerCase() + alignment.substring(1)); return <any>(alignment.charAt(0).toLowerCase() + alignment.substring(1));
} }
[iOSTabBarItemsAlignmentProperty.setNative](value: IOSTabBarItemsAlignment) { [iOSTabBarItemsAlignmentProperty.setNative](value: IOSTabBarItemsAlignment) {
if (!this.viewController || !this.viewController.tabBar) { if (!this.viewController || !this.viewController.tabBar) {
return; return;
@ -1219,4 +1225,4 @@ export class Tabs extends TabsBase {
this.viewController.tabBar.unselectedItemTintColor = this._unSelectedItemColor.ios; this.viewController.tabBar.unselectedItemTintColor = this._unSelectedItemColor.ios;
} }
} }
} }

View File

@ -1,18 +1,15 @@
/**
*
*/
package org.nativescript.widgets; package org.nativescript.widgets;
import android.content.Context; import android.content.Context;
import androidx.viewpager.widget.ViewPager; import androidx.viewpager.widget.ViewPager;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.KeyEvent; import android.view.KeyEvent;
// See this thread for more information https://stackoverflow.com/questions/9650265 // See this thread for more information https://stackoverflow.com/questions/9650265
public class TabViewPager extends ViewPager { public class TabViewPager extends ViewPager {
private boolean swipePageEnabled = true; private boolean swipePageEnabled = true;
private boolean animationEnabled = true;
public TabViewPager(Context context) { public TabViewPager(Context context) {
super(context); super(context);
@ -26,6 +23,10 @@ public class TabViewPager extends ViewPager {
this.swipePageEnabled = enabled; this.swipePageEnabled = enabled;
} }
public void setAnimationEnabled(boolean enabled) {
this.animationEnabled = enabled;
}
@Override @Override
public boolean onInterceptTouchEvent(MotionEvent event) { public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.swipePageEnabled) { if (this.swipePageEnabled) {
@ -55,6 +56,7 @@ public class TabViewPager extends ViewPager {
@Override @Override
public void setCurrentItem(int item) { public void setCurrentItem(int item) {
super.setCurrentItem(item, this.swipePageEnabled); boolean smoothScroll = this.animationEnabled && this.swipePageEnabled;
super.setCurrentItem(item, smoothScroll);
} }
} }