Tabris.js Widgets: ScrollView

The ScrollView is a composite widget, meaning it can contain other widgets like text and images. For those familiar with web development, the ScrollView provides the functionality we are used to finding on the window browser object; it is like a scrollable page. The content might overflow the ScrollView on the X-axis or on the Y-axis. The ScrollView gives the ability to scroll to this content horizontally or vertically.

This is an example of vertical scroll (scrollview-parallax.jsx – find it at https://playground.tabris.com/ or the GitHub repo). Note the scroll bar on the right hand side. This can be turned off by setting scrollbarVisible={false} on the ScrollView element.

Like the window object, we have the expected scroll events that fire on horizontal scroll ( onScrollX) and vertical scroll. Our handler functions have access to a read-only offset property.

In the example below, we use destructuring to access the offset of the scroll position on the ScrollView.

In addition, and specific to mobile development, is the idea of a scrollState. This can be accessed through the ScrollView’s scrollXState property and the scrollYState property. Possible values are ‘drag,’ ‘rest’ or ‘scroll,’ where drag refers to the user moving content with her finger on the touch screen device. ‘Scroll’ is when a ScrollView is scrolling programmatically , or continues to move after the user has dragged.

The following logs the ScrollState of the X-axis scroll:

onScrollXStateChanged and onScrollYStateChanged event listeners fire whenever the scroll state changes. In our event handlers we have access to the current scroll state.

The ability to scroll is determined by the size of the child elements. These must overflow the ScrollView in order for scrolling functionality to kick in.

Take the following example:

stretchX sets the ScrollView width to the width of the contentView, while the TextView width is left to its default of ‘auto’, which means it flows off the screen. However, if we add stretchX to the TextView, so it is the same width as the ScrollView there is no content to scroll to.

Also note that the dimensions of the ScrollView must be defined. According to the documentation – either the ScrollView’s width or left and right must be specified. In the above example if we remove the stretchX property that sets the ScrollView’s width, the overflow cannot calculate and, despite the text flowing off the screen, the scroll does not work.

Run this example on the Tabris developer app with the QR Code below: