Skip to content

Commit 40033eb

Browse files
authored
fix(android): keep the ListView sticky header layout listener out of a cycle (#11660)
The sticky header's native view holds the OnLayoutChangeListener that adjusts the list padding, and the listener's arrow function captured both its own variable (to remove itself) and the ListView. The self-reference outlived the listener's removal and the ListView reference reached the header's native view; wrapper resurrection used to collect both cycles, but a runtime that keeps a JS-implemented object's state for as long as Java holds the object cannot. The listener now removes itself through `this` and reaches the ListView through a WeakRef.
1 parent ef1971d commit 40033eb

2 files changed

Lines changed: 52 additions & 7 deletions

File tree

‎apps/automated/src/ui/list-view/list-view-tests.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,42 @@ export class ListViewTest extends UITest<ListView> {
872872
}
873873
}
874874

875+
public test_stickyHeader_Android_padding_after_garbage_collection() {
876+
if (!isAndroid) {
877+
return;
878+
}
879+
880+
this.setUp();
881+
882+
const listView = this.testView;
883+
listView.sectioned = true;
884+
listView.stickyHeader = true;
885+
listView.stickyHeaderTemplate = "<Label id='headerLabel' text='{{ title }}' />";
886+
listView.items = [
887+
{ title: 'First', items: ['a', 'b', 'c'] },
888+
{ title: 'Second', items: ['d', 'e'] },
889+
];
890+
891+
this.waitUntilTestElementIsLoaded();
892+
this.waitUntilTestElementLayoutIsValid();
893+
TKUnit.waitUntilReady(() => !!(<any>listView)._stickyHeaderView);
894+
895+
const header = <android.view.View>(<any>listView)._stickyHeaderView.nativeViewProtected;
896+
const nativeListView = <android.widget.ListView>listView.android;
897+
898+
// Registers a new layout listener, which has to survive the collections before it runs.
899+
(<any>listView)._addListViewPadding();
900+
for (let i = 0; i < 3; i++) {
901+
Utils.GC();
902+
java.lang.System.gc();
903+
java.lang.System.runFinalization();
904+
TKUnit.wait(0.05);
905+
}
906+
header.requestLayout();
907+
908+
TKUnit.waitUntilReady(() => nativeListView.getPaddingTop() === header.getMeasuredHeight() + 4);
909+
}
910+
875911
private checkItemVisibleAtIndex(listView: ListView, index: number): boolean {
876912
return listView.isItemAtIndexVisible(index);
877913
}

‎packages/core/ui/list-view/index.android.ts‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -458,25 +458,34 @@ export class ListView extends ListViewBase {
458458
// Then measure and adjust padding if needed using a layout listener for determinism
459459
const stickyHeaderNativeView = this._stickyHeaderView && this._stickyHeaderView.nativeViewProtected;
460460
if (stickyHeaderNativeView) {
461+
// The listener reaches neither itself nor the list view strongly: the header's
462+
// native view holds it, and both would be cycles through that native view.
463+
const owner = new WeakRef<ListView>(this);
461464
const layoutListener = new android.view.View.OnLayoutChangeListener({
462-
onLayoutChange: (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) => {
465+
onLayoutChange(v: android.view.View, left: number, top: number, right: number, bottom: number, oldLeft: number, oldTop: number, oldRight: number, oldBottom: number) {
466+
const listView = owner.deref();
467+
if (!listView) {
468+
v.removeOnLayoutChangeListener(this);
469+
470+
return;
471+
}
463472
if (v.getMeasuredHeight() > 0) {
464473
const measuredHeaderHeight = v.getMeasuredHeight();
465474
let finalSearchHeight = searchViewHeight;
466475
// Re-measure search view if needed
467-
if (this.showSearch && this._searchView && (this._searchView as any)._wrapper) {
468-
const searchWrapper = (this._searchView as any)._wrapper;
476+
if (listView.showSearch && listView._searchView && (listView._searchView as any)._wrapper) {
477+
const searchWrapper = (listView._searchView as any)._wrapper;
469478
if (searchWrapper.nativeViewProtected && searchWrapper.nativeViewProtected.getMeasuredHeight() > 0) {
470479
finalSearchHeight = searchWrapper.nativeViewProtected.getMeasuredHeight();
471480
}
472481
}
473482
// Calculate final padding: search height + sticky header height + small buffer
474483
const totalPaddingHeight = finalSearchHeight + measuredHeaderHeight + 4;
475-
this._stickyHeaderHeight = measuredHeaderHeight;
476-
this.nativeViewProtected.setPadding(0, totalPaddingHeight, 0, 0);
477-
this.scrollToIndex(0);
484+
listView._stickyHeaderHeight = measuredHeaderHeight;
485+
listView.nativeViewProtected.setPadding(0, totalPaddingHeight, 0, 0);
486+
listView.scrollToIndex(0);
478487
// Remove the listener after first valid layout
479-
v.removeOnLayoutChangeListener(layoutListener);
488+
v.removeOnLayoutChangeListener(this);
480489
}
481490
},
482491
});

0 commit comments

Comments
 (0)