mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
refactor(android-tabview): tab adapter data changed event is called too many times (#5364)
This commit is contained in:
@@ -414,11 +414,6 @@ export class TabView extends TabViewBase {
|
||||
this._viewPager = viewPager;
|
||||
this._pagerAdapter = (<any>viewPager).adapter;
|
||||
(<any>this._pagerAdapter).owner = this;
|
||||
|
||||
const items = this.items;
|
||||
if (items) {
|
||||
this.setAdapterItems(items)
|
||||
}
|
||||
}
|
||||
|
||||
public onLoaded(): void {
|
||||
@@ -434,9 +429,7 @@ export class TabView extends TabViewBase {
|
||||
}
|
||||
|
||||
public disposeNativeView() {
|
||||
(<any>this._pagerAdapter).items = null;
|
||||
this._tabLayout.setItems(null, null);
|
||||
this._pagerAdapter.notifyDataSetChanged();
|
||||
(<any>this._pagerAdapter).owner = null;
|
||||
this._pagerAdapter = null;
|
||||
|
||||
@@ -454,7 +447,41 @@ export class TabView extends TabViewBase {
|
||||
return false;
|
||||
}
|
||||
|
||||
private shouldUpdateAdapter(items: Array<TabViewItemDefinition>) {
|
||||
const currentPagerAdapterItems = (<any>this._pagerAdapter).items;
|
||||
|
||||
// if both values are null, should not update
|
||||
if (!items && !currentPagerAdapterItems) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// if one value is null, should update
|
||||
if (!items || !currentPagerAdapterItems) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// if both are Arrays but length doesn't match, should update
|
||||
if (items.length !== currentPagerAdapterItems.length) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const matchingItems = currentPagerAdapterItems.filter((currentItem) => {
|
||||
return !!items.filter((item) => {
|
||||
return item._domId === currentItem._domId
|
||||
})[0];
|
||||
});
|
||||
|
||||
// if both are Arrays and length matches, but not all items are the same, should update
|
||||
if (matchingItems.length !== items.length) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// if both are Arrays and length matches and all items are the same, should not update
|
||||
return false;
|
||||
}
|
||||
|
||||
private setAdapterItems(items: Array<TabViewItemDefinition>) {
|
||||
if (this.shouldUpdateAdapter(items)) {
|
||||
(<any>this._pagerAdapter).items = items;
|
||||
|
||||
const length = items ? items.length : 0;
|
||||
@@ -481,6 +508,7 @@ export class TabView extends TabViewBase {
|
||||
|
||||
this._pagerAdapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public updateAndroidItemAt(index: number, spec: org.nativescript.widgets.TabItemSpec) {
|
||||
this._tabLayout.updateItemAt(index, spec);
|
||||
|
||||
Reference in New Issue
Block a user