From 50d399101e654b686ded6195b35879f4edb6470b Mon Sep 17 00:00:00 2001 From: Alexander Vakrilov Date: Tue, 8 Aug 2017 13:06:56 +0300 Subject: [PATCH] Fixes TabView icons changes. (#4666) * Fixes TabView icons/title changes. * Access TabLayput trough parent * TabView icon change example --- apps/app/ui-tests-app/tab-view/main-page.ts | 1 + .../tab-view/tab-view-icon-change.ts | 17 +++++++++++++++++ .../tab-view/tab-view-icon-change.xml | 16 ++++++++++++++++ .../ui/tab-view/tab-view.android.ts | 19 ++++++++++++------- 4 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 apps/app/ui-tests-app/tab-view/tab-view-icon-change.ts create mode 100644 apps/app/ui-tests-app/tab-view/tab-view-icon-change.xml diff --git a/apps/app/ui-tests-app/tab-view/main-page.ts b/apps/app/ui-tests-app/tab-view/main-page.ts index 0cd5a0b7b..b7a917059 100644 --- a/apps/app/ui-tests-app/tab-view/main-page.ts +++ b/apps/app/ui-tests-app/tab-view/main-page.ts @@ -19,6 +19,7 @@ export function loadExamples() { examples.set("tabmore", "tab-view/tab-view-more"); examples.set("tabViewCss", "tab-view/tab-view-css"); examples.set("tab-view-icons", "tab-view/tab-view-icon"); + examples.set("tab-view-icon-change", "tab-view/tab-view-icon-change"); examples.set("text-transform", "tab-view/text-transform"); return examples; } diff --git a/apps/app/ui-tests-app/tab-view/tab-view-icon-change.ts b/apps/app/ui-tests-app/tab-view/tab-view-icon-change.ts new file mode 100644 index 000000000..6ebd0d4fb --- /dev/null +++ b/apps/app/ui-tests-app/tab-view/tab-view-icon-change.ts @@ -0,0 +1,17 @@ +import { EventData } from "tns-core-modules/data/observable"; +import { Button } from "tns-core-modules/ui/button"; +import { TabView, SelectedIndexChangedEventData } from "tns-core-modules/ui/tab-view"; + +export function onSelectedIndexChanged(args: SelectedIndexChangedEventData) { + const tabView = args.object as TabView; + + const newItem = tabView.items[args.newIndex]; + if (newItem) { + newItem.iconSource = "res://icon"; + } + + const oldItem = tabView.items[args.oldIndex]; + if (oldItem) { + oldItem.iconSource = "res://testlogo"; + } +} diff --git a/apps/app/ui-tests-app/tab-view/tab-view-icon-change.xml b/apps/app/ui-tests-app/tab-view/tab-view-icon-change.xml new file mode 100644 index 000000000..a4de225af --- /dev/null +++ b/apps/app/ui-tests-app/tab-view/tab-view-icon-change.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tns-core-modules/ui/tab-view/tab-view.android.ts b/tns-core-modules/ui/tab-view/tab-view.android.ts index 0bbf049f9..39db149eb 100644 --- a/tns-core-modules/ui/tab-view/tab-view.android.ts +++ b/tns-core-modules/ui/tab-view/tab-view.android.ts @@ -19,11 +19,11 @@ const PRIMARY_COLOR = "colorPrimary"; const DEFAULT_ELEVATION = 4; interface PagerAdapter { - new (owner: TabView, items: Array): android.support.v4.view.PagerAdapter; + new(owner: TabView, items: Array): android.support.v4.view.PagerAdapter; } interface PageChangedListener { - new (owner: TabView): android.support.v4.view.ViewPager.SimpleOnPageChangeListener; + new(owner: TabView): android.support.v4.view.ViewPager.SimpleOnPageChangeListener; } let PagerAdapter: PagerAdapter; @@ -87,7 +87,7 @@ function initializeNativeClasses() { container.removeView(nativeView); // Note: this.owner._removeView will clear item.view.nativeView. - // So call this after the native instance is removed form the container. + // So call this after the native instance is removed form the container. // if (item.view.parent === this.owner) { // this.owner._removeView(item.view); // } @@ -208,9 +208,10 @@ export class TabViewItem extends TabViewItemBase { public _update(): void { const tv = this.nativeViewProtected; - if (tv) { - const tabLayout = tv.getParent(); - tabLayout.updateItemAt(this.index, this.tabItemSpec); + const tabView = this.parent as TabView; + if (tv && tabView) { + this.tabItemSpec = createTabItemSpec(this); + tabView.updateAndroidItemAt(this.index, this.tabItemSpec); } } @@ -313,7 +314,7 @@ export class TabView extends TabViewBase { const listener = new PageChangedListener(this); (viewPager).addOnPageChangeListener(listener); (viewPager).listener = listener; - + const adapter = new PagerAdapter(this, null); viewPager.setAdapter(adapter); (viewPager).adapter = adapter; @@ -382,6 +383,10 @@ export class TabView extends TabViewBase { this._pagerAdapter.notifyDataSetChanged(); } + public updateAndroidItemAt(index: number, spec: org.nativescript.widgets.TabItemSpec) { + this._tabLayout.updateItemAt(index, spec); + } + [androidOffscreenTabLimitProperty.getDefault](): number { return this._viewPager.getOffscreenPageLimit(); }