Skip to content

Commit 2f92b50

Browse files
committed
fix(android): keep the ListView sticky header layout listener out of a cycle
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 283eb0c commit 2f92b50

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
@@ -460,25 +460,34 @@ export class ListView extends ListViewBase {
460460
// Then measure and adjust padding if needed using a layout listener for determinism
461461
const stickyHeaderNativeView = this._stickyHeaderView && this._stickyHeaderView.nativeViewProtected;
462462
if (stickyHeaderNativeView) {
463+
// The listener reaches neither itself nor the list view strongly: the header's
464+
// native view holds it, and both would be cycles through that native view.
465+
const owner = new WeakRef<ListView>(this);
463466
const layoutListener = new android.view.View.OnLayoutChangeListener({
464-
onLayoutChange: (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) => {
467+
onLayoutChange(v: android.view.View, left: number, top: number, right: number, bottom: number, oldLeft: number, oldTop: number, oldRight: number, oldBottom: number) {
468+
const listView = owner.deref();
469+
if (!listView) {
470+
v.removeOnLayoutChangeListener(this);
471+
472+
return;
473+
}
465474
if (v.getMeasuredHeight() > 0) {
466475
const measuredHeaderHeight = v.getMeasuredHeight();
467476
let finalSearchHeight = searchViewHeight;
468477
// Re-measure search view if needed
469-
if (this.showSearch && this._searchView && (this._searchView as any)._wrapper) {
470-
const searchWrapper = (this._searchView as any)._wrapper;
478+
if (listView.showSearch && listView._searchView && (listView._searchView as any)._wrapper) {
479+
const searchWrapper = (listView._searchView as any)._wrapper;
471480
if (searchWrapper.nativeViewProtected && searchWrapper.nativeViewProtected.getMeasuredHeight() > 0) {
472481
finalSearchHeight = searchWrapper.nativeViewProtected.getMeasuredHeight();
473482
}
474483
}
475484
// Calculate final padding: search height + sticky header height + small buffer
476485
const totalPaddingHeight = finalSearchHeight + measuredHeaderHeight + 4;
477-
this._stickyHeaderHeight = measuredHeaderHeight;
478-
this.nativeViewProtected.setPadding(0, totalPaddingHeight, 0, 0);
479-
this.scrollToIndex(0);
486+
listView._stickyHeaderHeight = measuredHeaderHeight;
487+
listView.nativeViewProtected.setPadding(0, totalPaddingHeight, 0, 0);
488+
listView.scrollToIndex(0);
480489
// Remove the listener after first valid layout
481-
v.removeOnLayoutChangeListener(layoutListener);
490+
v.removeOnLayoutChangeListener(this);
482491
}
483492
},
484493
});

0 commit comments

Comments
 (0)