Skip to content

Commit 65c8163

Browse files
authored
fix(android): ListView margin handling on layout containers (#10937)
1 parent 31f7427 commit 65c8163

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ItemEventData, ItemsSource } from '.';
22
import { ListViewBase, separatorColorProperty, itemTemplatesProperty } from './list-view-common';
33
import { View, KeyedTemplate } from '../core/view';
4+
import { PercentLength } from '../styling/length-shared';
45
import { unsetValue } from '../core/properties/property-shared';
56
import { CoreTypes } from '../../core-types';
67
import { Color } from '../../color';
@@ -404,11 +405,28 @@ function ensureListViewAdapterClass() {
404405

405406
this.owner._prepareItem(args.view, index);
406407
if (!args.view.parent) {
407-
// Proxy containers should not get treated as layouts.
408-
// Wrap them in a real layout as well.
408+
// Ensure margins defined on the template root are honored on Android ListView.
409+
// ListView's children don't support layout margins, so we insert an outer wrapper
410+
// and keep the original view (with its margins) inside. This mirrors iOS spacing.
411+
412+
// If the view is already a LayoutBase (typical for templates like GridLayout),
413+
// we wrap it so its margins take effect. For non-layout roots (labels, etc.)
414+
// we already wrap below with a StackLayout.
409415
if (args.view instanceof LayoutBase && !(args.view instanceof ProxyViewContainer)) {
410-
this.owner._addView(args.view);
411-
convertView = args.view.nativeViewProtected;
416+
const mt = PercentLength.toDevicePixels(args.view.marginTop, 0, Number.NaN);
417+
const mb = PercentLength.toDevicePixels(args.view.marginBottom, 0, Number.NaN);
418+
const ml = PercentLength.toDevicePixels(args.view.marginLeft, 0, Number.NaN);
419+
const mr = PercentLength.toDevicePixels(args.view.marginRight, 0, Number.NaN);
420+
const hasMargins = mt > 0 || mb > 0 || ml > 0 || mr > 0;
421+
if (hasMargins) {
422+
const outer = new StackLayout();
423+
outer.addChild(args.view);
424+
this.owner._addView(outer);
425+
convertView = outer.nativeViewProtected;
426+
} else {
427+
this.owner._addView(args.view);
428+
convertView = args.view.nativeViewProtected;
429+
}
412430
} else {
413431
const sp = new StackLayout();
414432
sp.addChild(args.view);

0 commit comments

Comments
 (0)