Skip to content

Commit 745739a

Browse files
committed
Add native ScrollView backing, wrapping multi-child content in one inner LinearLayout
1 parent 6f651da commit 745739a

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// AndroidScrollView.swift
3+
// AndroidSwiftUI
4+
//
5+
// Created by Alsey Coleman Miller on 7/16/26.
6+
//
7+
8+
import AndroidKit
9+
10+
extension ScrollView: AndroidPrimitive {
11+
12+
var renderedBody: AnyView {
13+
AnyView(AndroidScrollContainer(axes: axes, content: content))
14+
}
15+
}
16+
17+
/// Native container for `ScrollView`. Android's `ScrollView`/`HorizontalScrollView` accept only a
18+
/// single direct child, so `content` (which may expand to multiple top-level views, e.g. `ScrollView { A; B }`)
19+
/// is wrapped in a single inner `AndroidLinearLayout`.
20+
struct AndroidScrollContainer<Content: View> {
21+
22+
let axes: Axis.Set
23+
24+
let content: Content
25+
}
26+
27+
extension AndroidScrollContainer: ParentView {
28+
29+
var children: [AnyView] {
30+
let orientation: AndroidLinearLayout.Orientation = axes.contains(.horizontal) ? .horizontal : .vertical
31+
return [AnyView(AndroidLinearLayout(orientation: orientation, gravity: .noGravity) { content })]
32+
}
33+
}
34+
35+
extension AndroidScrollContainer: AndroidViewRepresentable {
36+
37+
typealias Coordinator = Void
38+
39+
func makeAndroidView(context: Self.Context) -> AndroidView.View {
40+
if axes.contains(.horizontal) {
41+
return AndroidWidget.HorizontalScrollView(context.androidContext)
42+
} else {
43+
return AndroidWidget.ScrollView(context.androidContext)
44+
}
45+
}
46+
47+
func updateAndroidView(_ view: AndroidView.View, context: Self.Context) {
48+
49+
}
50+
}

0 commit comments

Comments
 (0)