ScrollView

API Reference: UI.ScrollView

ScrollView enables the user to view pages with content longer than the screen size via a scroll action.

TypeScript code blocks include examples of how to implement, override and components within the theme. You can create page with the UI Editor to make your page compatible with theming and then you can implement themable components programmatically. Once the page is created with the UI Editor, it generates a class under scripts/generated/pages. You can then extend that class with the following TypeScript classes.

TypeScript
JavaScript
TypeScript
import PageSampleDesign from 'generated/pages/pageSample';
import FlexLayout = require('sf-core/ui/flexlayout');
import Application = require('sf-core/application');
import RangeSlider = require('sf-core/ui/rangeslider');
import Color = require('sf-core/ui/color');
import ScrollView = require('sf-core/ui/scrollview');
import componentContextPatch from '@smartface/contx/lib/smartface/componentContextPatch';
//You should create new Page from UI-Editor and extend with it.
export default class Sample extends PageSampleDesign {
colors: string[] = ["#F25022", "#7FBA00", "#00A4EF", "#FFB900", "#6441A4"];
myScrollView: ScrollView;
constructor() {
super();
// Overrides super.onShow method
this.onShow = onShow.bind(this, this.onShow.bind(this));
// Overrides super.onLoad method
this.onLoad = onLoad.bind(this, this.onLoad.bind(this));
this.layout.flexDirection = FlexLayout.FlexDirection.COLUMN;
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
this.layout.alignItems = FlexLayout.AlignItems.CENTER;
}
}
/**
* @event onShow
* This event is called when a page appears on the screen (everytime).
* @param {function} superOnShow super onShow function
* @param {Object} parameters passed from Router.go function
*/
function onShow(superOnShow: () => void) {
const { headerBar } = this;
superOnShow();
Application.statusBar.visible = false;
headerBar.visible = false;
}
/**
* @event onLoad
* This event is called once when page is created.
* @param {function} superOnLoad super onLoad function
*/
function onLoad(superOnLoad: () => void) {
superOnLoad();
this.colors = ["#F25022", "#7FBA00", "#00A4EF", "#FFB900", "#6441A4"];
this.myScrollView = new ScrollView({
autoSizeEnabled: true
});
componentContextPatch(this.myScrollView.layout, "myScrollViewLayout");
for (let i = 0; i < 10; i++) {
let flex = new FlexLayout();
this.myScrollView.layout.addChild(flex, "flex", ".sf-flexLayout", {
height: 100,
backgroundColor: this.colors[i % 5]
});
};
this.layout.addChild(this.myScrollView, "myScrollView", null, {
width: 400,
backgroundColor: "#00A1F1"
});
this.myScrollView.layout.applyLayout();
}
JavaScript
const Page = require("sf-core/ui/page");
const extend = require("js-base/core/extend");
const FlexLayout = require('sf-core/ui/flexlayout');
const Color = require('sf-core/ui/color');
const ScrollView = require('sf-core/ui/scrollview');
var Page1 = extend(Page)(
function(_super) {
_super(this, {
onShow: function(params) {
Application.statusBar.visible = false;
this.headerBar.visible = false;
},orientation : Page.Orientation.AUTO
});
var colors = ["#F25022","#7FBA00","#00A4EF","#FFB900","#6441A4"];
var myScrollView = new ScrollView({
flexGrow: 1,
margin: 80,
backgroundColor: Color.create("#00A1F1"),
align: ScrollView.Align.VERTICAL,
autoSizeEnabled : true
});
for (var i = 0; i < 10; i++) {
var flex = new FlexLayout();
flex.height = 100;
flex.backgroundColor = Color.create(colors[i%5]);
myScrollView.layout.addChild(flex);
};
this.layout.addChild(myScrollView);
myScrollView.layout.applyLayout();
}
);
module.exports = Page1;

Content Auto Sizing on ScrollView

To do the automatic calculation, you need to set scrollview.autoSizeEnabled property true and need to call scrollview.layout.applyLayout() function after every change.

Calculated auto size will be least ScrollView's size.

Usage

  • if autoSizeEnabled property is false, scrollView layout's children should have a fixed or minimum height for vertical scrolling.

  • if autoSizeEnabled property is false, ScrollView layout's children should have a fixed or minimum width for horizontal scrolling.