fix-next(android-tabview): handle tab fragments attach/detach properly (#5333)

This commit is contained in:
Martin Yankov
2018-01-29 17:25:36 +02:00
committed by Alexander Vakrilov
parent 01fab68e58
commit 2b843195e4
2 changed files with 41 additions and 22 deletions

View File

@@ -740,7 +740,6 @@ class FragmentCallbacksImplementation implements AndroidFragmentCallbacks {
@profile @profile
public onStop(fragment: android.app.Fragment, superFunc: Function): void { public onStop(fragment: android.app.Fragment, superFunc: Function): void {
superFunc.call(fragment); superFunc.call(fragment);
this.entry.resolvedPage.callUnloaded();
} }
@profile @profile

View File

@@ -78,7 +78,11 @@ function initializeNativeClasses() {
} }
} }
const POSITION_UNCHANGED = -1;
const POSITION_NONE = -2;
class FragmentPagerAdapter extends android.support.v4.view.PagerAdapter { class FragmentPagerAdapter extends android.support.v4.view.PagerAdapter {
public items: Array<TabViewItemDefinition>;
private mCurTransaction: android.app.FragmentTransaction; private mCurTransaction: android.app.FragmentTransaction;
private mCurrentPrimaryItem: android.app.Fragment; private mCurrentPrimaryItem: android.app.Fragment;
@@ -88,12 +92,12 @@ function initializeNativeClasses() {
} }
getCount() { getCount() {
const items = this.owner.items; const items = this.items;
return items ? items.length : 0; return items ? items.length : 0;
} }
getPageTitle(index: number) { getPageTitle(index: number) {
const items = this.owner.items; const items = this.items;
if (index < 0 || index >= items.length) { if (index < 0 || index >= items.length) {
return ""; return "";
} }
@@ -129,15 +133,19 @@ function initializeNativeClasses() {
fragment.setUserVisibleHint(false); fragment.setUserVisibleHint(false);
} }
const items = this.owner.items; const tabItems = this.owner.items;
const item = items ? items[position] : null; const tabItem = tabItems ? tabItems[position] : null;
if (item) { if (tabItem) {
item.canBeLoaded = true; tabItem.canBeLoaded = true;
} }
return fragment; return fragment;
} }
getItemPosition(object: java.lang.Object): number {
return this.items ? POSITION_UNCHANGED : POSITION_NONE;
}
destroyItem(container: android.view.ViewGroup, position: number, object: java.lang.Object): void { destroyItem(container: android.view.ViewGroup, position: number, object: java.lang.Object): void {
if (!this.mCurTransaction) { if (!this.mCurTransaction) {
const fragmentManager = this.owner._getFragmentManager(); const fragmentManager = this.owner._getFragmentManager();
@@ -147,10 +155,10 @@ function initializeNativeClasses() {
const fragment: android.app.Fragment = <android.app.Fragment>object; const fragment: android.app.Fragment = <android.app.Fragment>object;
this.mCurTransaction.detach(fragment); this.mCurTransaction.detach(fragment);
const items = this.owner.items; const tabItems = this.owner.items;
const item = items ? items[position] : null; const tabItem = tabItems ? tabItems[position] : null;
if (item) { if (tabItem) {
item.canBeLoaded = false; tabItem.canBeLoaded = false;
} }
} }
@@ -169,13 +177,13 @@ function initializeNativeClasses() {
this.mCurrentPrimaryItem = fragment; this.mCurrentPrimaryItem = fragment;
this.owner.selectedIndex = position; this.owner.selectedIndex = position;
}
const tab = this.owner;
const items = tab.items; const tab = this.owner;
const newItem = items ? items[position] : null; const tabItems = tab.items;
if (newItem && tab.isLoaded) { const newTabItem = tabItems ? tabItems[position] : null;
newItem.loadView(newItem.view); if (newTabItem && tab.isLoaded) {
} newTabItem.loadView(newTabItem.view);
} }
} }
@@ -253,6 +261,10 @@ export class TabViewItem extends TabViewItemBase {
} }
} }
public onLoaded(): void {
super.onLoaded();
}
public resetNativeView(): void { public resetNativeView(): void {
super.resetNativeView(); super.resetNativeView();
if (this.nativeViewProtected) { if (this.nativeViewProtected) {
@@ -409,6 +421,18 @@ export class TabView extends TabViewBase {
} }
} }
public onLoaded(): void {
super.onLoaded();
this.setAdapterItems(this.items);
}
public onUnloaded(): void {
super.onUnloaded();
this.setAdapterItems(null);
}
public disposeNativeView() { public disposeNativeView() {
(<any>this._pagerAdapter).items = null; (<any>this._pagerAdapter).items = null;
this._tabLayout.setItems(null, null); this._tabLayout.setItems(null, null);
@@ -448,10 +472,6 @@ export class TabView extends TabViewBase {
tabItems.push(tabItemSpec); tabItems.push(tabItemSpec);
}); });
// // TODO: optimize by reusing the adapter and calling setAdapter(null) then the same adapter.
// this._pagerAdapter = new PagerAdapter(this, items);
// this._viewPager.setAdapter(this._pagerAdapter);
const tabLayout = this._tabLayout; const tabLayout = this._tabLayout;
tabLayout.setItems(tabItems, this._viewPager); tabLayout.setItems(tabItems, this._viewPager);
items.forEach((item, i, arr) => { items.forEach((item, i, arr) => {