|
1 | 1 | ---
|
2 | 2 | title: ScrollView
|
| 3 | +description: UI component for rendering scrollable content (horizontal or vertical). |
3 | 4 | ---
|
4 | 5 |
|
5 |
| -`<ScrollView>` is a UI component that shows a scrollable content area. Content can be scrolled vertically or horizontally. |
| 6 | +`<ScrollView>` is a UI component for rendering scrollable content. Content can be scrolled `vertically` (default) or `horizontally`. |
6 | 7 |
|
7 |
| -It's important to note that `<ScrollView>` extends [`ContentView`](https://docs.nativescript.org/api-reference/classes/contentview), so it can only have a single child element. |
| 8 | +::: warning Note |
8 | 9 |
|
9 |
| -By default, ScrollView scrolls vertically. To scroll horizontally, set the ScrollView's `orientation` property to `horizontal`. |
| 10 | +A ScrollView can only have a single child element. |
10 | 11 |
|
11 |
| ---- |
| 12 | +::: |
12 | 13 |
|
13 | 14 | <DeviceFrame type="ios">
|
14 |
| -<img src="https://raw.githubusercontent.com/nativescript-vue/nativescript-vue-ui-tests/master/screenshots/ios-simulator103iPhone6/ScrollView.png"/> |
| 15 | +<img src="../screenshots/ios/ScrollView.png"/> |
15 | 16 | </DeviceFrame>
|
16 | 17 | <DeviceFrame type="android">
|
17 |
| -<img src="https://raw.githubusercontent.com/nativescript-vue/nativescript-vue-ui-tests/master/screenshots/android23/ScrollView.png" /> |
| 18 | +<img src="../screenshots/android/ScrollView.png"/> |
18 | 19 | </DeviceFrame>
|
19 | 20 |
|
20 |
| -### Creating a Simple ScrollView |
21 |
| - |
22 |
| -<!-- /// flavor plain --> |
23 |
| - |
24 |
| -```xml |
25 |
| - |
26 |
| -<ScrollView scroll="onScroll"> |
27 |
| - <GridLayout rows="200 200 200 200 200 200 200 200 200 200"> |
28 |
| - <Label row="0" text="Some row content goes here..." /> |
29 |
| - <Label row="1" text="Some row content goes here..." /> |
30 |
| - <Label row="2" text="Some row content goes here..." /> |
31 |
| - <Label row="3" text="Some row content goes here..." /> |
32 |
| - <Label row="4" text="Some row content goes here..." /> |
33 |
| - <Label row="5" text="Some row content goes here..." /> |
34 |
| - <Label row="6" text="Some row content goes here..." /> |
35 |
| - <Label row="7" text="Some row content goes here..." /> |
36 |
| - <Label row="8" text="Some row content goes here..." /> |
37 |
| - <Label row="9" text="Some row content goes here..." /> |
38 |
| - </GridLayout> |
39 |
| -</ScrollView> |
40 |
| -``` |
41 |
| - |
42 |
| -```ts |
43 |
| -import { Page, ScrollEventData, ScrollView } from '@nativescript/core' |
44 |
| - |
45 |
| -export function onScroll(args: ScrollEventData) { |
46 |
| - const scrollView = args.object as ScrollView |
47 |
| - |
48 |
| - console.log('scrollX: ' + args.scrollX) |
49 |
| - console.log('scrollY: ' + args.scrollY) |
50 |
| -} |
51 |
| -``` |
52 |
| - |
53 |
| -<!-- /// |
54 |
| -
|
55 |
| -/// flavor angular |
56 |
| -
|
57 |
| -```xml |
58 |
| -<ScrollView (scroll)="onScroll($event)"> |
59 |
| - <GridLayout rows="200 200 200 200 200 200 200 200 200 200"> |
60 |
| - <Label row="0" text="Some row content goes here..."></label> |
61 |
| - <Label row="1" text="Some row content goes here..."></label> |
62 |
| - <Label row="2" text="Some row content goes here..."></label> |
63 |
| - <Label row="3" text="Some row content goes here..."></label> |
64 |
| - <Label row="4" text="Some row content goes here..."></label> |
65 |
| - <Label row="5" text="Some row content goes here..."></label> |
66 |
| - <Label row="6" text="Some row content goes here..."></label> |
67 |
| - <Label row="7" text="Some row content goes here..."></label> |
68 |
| - <Label row="8" text="Some row content goes here..."></label> |
69 |
| - <Label row="9" text="Some row content goes here..."></label> |
70 |
| - </GridLayout> |
71 |
| -</ScrollView> |
72 |
| -``` |
73 |
| -
|
74 |
| -```ts |
75 |
| -import { Component } from '@angular/core' |
76 |
| -import { ScrollView, ScrollEventData } from '@nativescript/core' |
77 |
| -
|
78 |
| -@Component({ |
79 |
| - moduleId: module.id, |
80 |
| - templateUrl: './tips-and-tricks.component.html' |
81 |
| -}) |
82 |
| -export class TipsAndTricksComponent { |
83 |
| - onScroll(args: ScrollEventData) { |
84 |
| - const scrollView = args.object as ScrollView |
85 |
| -
|
86 |
| - console.log('scrollX: ' + args.scrollX) |
87 |
| - console.log('scrollY: ' + args.scrollY) |
88 |
| - } |
89 |
| -} |
90 |
| -``` |
91 |
| -
|
92 |
| -/// |
93 |
| -
|
94 |
| -/// flavor vue |
95 |
| -
|
96 |
| -```xml |
97 |
| -<ScrollView orientation="horizontal"> |
98 |
| - <StackLayout orientation="horizontal"> |
99 |
| - <Label text="this" /> |
100 |
| - <Label text="text" /> |
101 |
| - <Label text="scrolls" /> |
102 |
| - <Label text="horizontally" /> |
103 |
| - <Label text="if necessary" /> |
104 |
| - </StackLayout> |
105 |
| -</ScrollView> |
106 |
| -``` |
107 |
| -
|
108 |
| -/// |
109 |
| -
|
110 |
| -/// flavor svelte |
111 |
| -
|
112 |
| -```xml |
113 |
| -<scrollView orientation="horizontal"> |
114 |
| - <stackLayout orientation="horizontal"> |
115 |
| - <Label text="this" /> |
116 |
| - <Label text="text" /> |
117 |
| - <Label text="scrolls" /> |
118 |
| - <Label text="horizontally" /> |
119 |
| - <Label text="if necessary" /> |
120 |
| - </stackLayout> |
121 |
| -</scrollView> |
122 |
| -``` |
123 |
| -
|
124 |
| -/// |
125 |
| -
|
126 |
| -/// flavor react |
127 |
| -
|
128 |
| -```xml |
129 |
| -<scrollView orientation="horizontal"> |
130 |
| - <stackLayout orientation="horizontal"> |
131 |
| - <Label text="this" /> |
132 |
| - <Label text="text" /> |
133 |
| - <Label text="scrolls" /> |
134 |
| - <Label text="horizontally" /> |
135 |
| - <Label text="if necessary" /> |
136 |
| - </stackLayout> |
137 |
| -</scrollView> |
138 |
| -``` |
139 |
| -
|
140 |
| -/// |
141 |
| ---> |
| 21 | +<<< @/../examples/src/ui/ScrollView/template.xml#example |
142 | 22 |
|
143 | 23 | ## Props
|
144 | 24 |
|
145 | 25 | ### orientation
|
146 | 26 |
|
147 |
| -```xml |
148 |
| - <ScrollView orientation="horizontal"> |
| 27 | +```ts |
| 28 | +orientation: 'horizontal' | 'vertical' |
149 | 29 | ```
|
150 | 30 |
|
151 |
| -Gets or sets the direction in which the content can be scrolled: `horizontal` or `vertical`. Defaults to `vertical`. |
| 31 | +Gets or sets the direction in which the content can be scrolled. |
152 | 32 |
|
153 |
| ---- |
| 33 | +Defaults to `vertical`. |
154 | 34 |
|
155 | 35 | ### scrollBarIndicatorVisible
|
156 | 36 |
|
157 |
| -```xml |
158 |
| - <ScrollView scrollBarIndicatorVisible="false"> |
| 37 | +```ts |
| 38 | +scrollBarIndicatorVisible: boolean |
159 | 39 | ```
|
160 | 40 |
|
161 |
| -Specifies if the scrollbar is visible. Defaults to `true`. |
| 41 | +Specifies if the scrollbar is visible. |
162 | 42 |
|
163 |
| ---- |
| 43 | +Defaults to `true`. |
164 | 44 |
|
165 | 45 | ### isScrollEnabled
|
166 | 46 |
|
167 |
| -```xml |
168 |
| - <ScrollView isScrollEnabled="false"> |
| 47 | +```ts |
| 48 | +isScrollEnabled: boolean |
169 | 49 | ```
|
170 | 50 |
|
171 |
| -Gets or sets a value indicating whether scroll is enabled. |
172 |
| - |
173 |
| ---- |
| 51 | +Enables or disables scrolling of the ScrollView. |
174 | 52 |
|
175 |
| -### verticalOffset |
| 53 | +### verticalOffset {#verticalOffset} |
176 | 54 |
|
177 | 55 | ```ts
|
178 |
| -verticalOffset: number = scrollView.verticalOffset |
| 56 | +verticalOffset: number |
179 | 57 | ```
|
180 | 58 |
|
181 |
| -Gets a value that contains the vertical offset of the scrolled content. |
| 59 | +Gets the vertical offset of the scrolled content. |
182 | 60 |
|
183 |
| ---- |
184 |
| - |
185 |
| -### horizontalOffset |
| 61 | +### horizontalOffset {#horizontalOffset} |
186 | 62 |
|
187 | 63 | ```ts
|
188 |
| -horizontalOffset: number = scrollView.horizontalOffset |
| 64 | +horizontalOffset: number |
189 | 65 | ```
|
190 | 66 |
|
191 |
| -Gets a value that contains the horizontal offset of the scrolled content. |
192 |
| - |
193 |
| ---- |
| 67 | +Gets the horizontal offset of the scrolled content. |
194 | 68 |
|
195 | 69 | ### scrollableHeight
|
196 | 70 |
|
197 | 71 | ```ts
|
198 |
| -scrollableHeight: number = scrollView.scrollableHeight |
| 72 | +scrollableHeight: number |
199 | 73 | ```
|
200 | 74 |
|
201 |
| -Gets the maximum value for the [verticalOffset](#verticaloffset). |
202 |
| - |
203 |
| ---- |
| 75 | +Gets the maximum scrollable height, this is also the maximum value for the [verticalOffset](#verticalOffset). |
204 | 76 |
|
205 | 77 | ### scrollableWidth
|
206 | 78 |
|
207 | 79 | ```ts
|
208 | 80 | scrollableWidth: number = scrollView.scrollableWidth
|
209 | 81 | ```
|
210 | 82 |
|
211 |
| -Gets the maximum value for the [horizontalOffset](#horizontaloffset). |
| 83 | +Gets the maximum scrollable width, this is also the maximum value for the [horizontalOffset](#horizontalOffset). |
212 | 84 |
|
213 |
| ---- |
| 85 | +## Methods |
214 | 86 |
|
215 | 87 | ### scrollToVerticalOffset()
|
216 | 88 |
|
217 | 89 | ```ts
|
218 |
| -scrollView.scrollToVerticalOffset(value: number, animated: boolean) |
| 90 | +scrollToVerticalOffset(value: number, animated: boolean) |
219 | 91 | ```
|
220 | 92 |
|
221 |
| -Scrolls the content to the specified vertical offset position. Set `animated` to `true` for animated scroll, `false` for immediate scroll. |
| 93 | +Scrolls the content to the specified vertical offset. |
222 | 94 |
|
223 |
| ---- |
| 95 | +Set `animated` to `true` for animated scroll, `false` for immediate scroll. |
224 | 96 |
|
225 | 97 | ### scrollToHorizontalOffset()
|
226 | 98 |
|
227 | 99 | ```ts
|
228 |
| -scrollView.scrollToHorizontalOffset(value: number, animated: boolean) |
| 100 | +scrollToHorizontalOffset(value: number, animated: boolean) |
229 | 101 | ```
|
230 | 102 |
|
231 |
| -Scrolls the content to the specified horizontal offset position. Set `animated` to `true` for animated scroll, `false` for immediate scroll. |
| 103 | +Scrolls the content to the specified horizontal offset position. |
232 | 104 |
|
233 |
| ---- |
| 105 | +Set `animated` to `true` for animated scroll, `false` for immediate scroll. |
234 | 106 |
|
235 | 107 | ## Events
|
236 | 108 |
|
237 | 109 | ### scroll
|
238 | 110 |
|
239 |
| -Emitted when a scroll event occurs. For the event's data, see [ScrollEventData Interface](https://docs.nativescript.org/api-reference/interfaces/scrolleventdata). |
| 111 | +```ts |
| 112 | +on('scroll', (args: ScrollEventData) => { |
| 113 | + const scrollView = args.object as ScrollView |
| 114 | + console.log('Scrolled', { |
| 115 | + scrollX: args.scrollX, |
| 116 | + scrollY: args.scrollY, |
| 117 | + }) |
| 118 | +}) |
| 119 | +``` |
240 | 120 |
|
241 |
| ---- |
| 121 | +Emitted when the ScrollView is scrolled. |
| 122 | + |
| 123 | +See [ScrollEventData](/api/interface/ScrollEventData). |
242 | 124 |
|
243 | 125 | ## Native component
|
244 | 126 |
|
|
0 commit comments