Setting the vertical scroll position on a Spark List control in Flex 4

The following example shows how you can set the vertical scroll position on a Spark List control in Flex 4 by setting the verticalScrollPosition property on the VerticalLayout object.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/05/30/setting-the-vertical-scroll-position-on-a-spark-list-control-in-flex-4/ -->
<s:Application name="Spark_List_layout_VerticalLayout_verticalScrollPosition_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <mx:Form styleName="plain">
            <mx:FormItem label="verticalScrollPosition:">
                <s:HSlider id="slider" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
 
            protected function list_creationCompleteHandler(evt:FlexEvent):void {
                slider.maximum = list.dataGroup.contentHeight - list.height;
            }
        ]]>
    </fx:Script>
 
    <s:List id="list"
            horizontalCenter="0"
            verticalCenter="0"
            creationComplete="list_creationCompleteHandler(event);">
        <s:layout>
            <s:VerticalLayout horizontalAlign="justify" 
                    requestedRowCount="4"
                    verticalScrollPosition="{slider.value}" />
        </s:layout>
        <s:dataProvider>
            <s:ArrayList>
                <fx:String>The</fx:String>
                <fx:String>Quick</fx:String>
                <fx:String>Brown</fx:String>
                <fx:String>Fox</fx:String>
                <fx:String>Jumps</fx:String>
                <fx:String>Over</fx:String>
                <fx:String>The</fx:String>
                <fx:String>Lazy</fx:String>
                <fx:String>Dog</fx:String>
            </s:ArrayList>
        </s:dataProvider>
    </s:List>
 
</s:Application>

View source is enabled in the following example.

You can also set the verticalScrollPosition property using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/05/30/setting-the-vertical-scroll-position-on-a-spark-list-control-in-flex-4/ -->
<s:Application name="Spark_List_layout_VerticalLayout_verticalScrollPosition_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <mx:Form styleName="plain">
            <mx:FormItem label="verticalScrollPosition:">
                <s:HSlider id="slider"
                        change="slider_changeHandler(event)" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
 
            protected function list_creationCompleteHandler(evt:FlexEvent):void {
                slider.maximum = list.dataGroup.contentHeight - list.height;
            }
 
            protected function slider_changeHandler(evt:Event):void {
                list.layout.verticalScrollPosition = slider.value;
            }
        ]]>
    </fx:Script>
 
    <s:List id="list"
            horizontalCenter="0"
            verticalCenter="0"
            creationComplete="list_creationCompleteHandler(event);">
        <s:layout>
            <s:VerticalLayout horizontalAlign="justify" 
                    requestedRowCount="4" />
        </s:layout>
        <s:dataProvider>
            <s:ArrayList>
                <fx:String>The</fx:String>
                <fx:String>Quick</fx:String>
                <fx:String>Brown</fx:String>
                <fx:String>Fox</fx:String>
                <fx:String>Jumps</fx:String>
                <fx:String>Over</fx:String>
                <fx:String>The</fx:String>
                <fx:String>Lazy</fx:String>
                <fx:String>Dog</fx:String>
            </s:ArrayList>
        </s:dataProvider>
    </s:List>
 
</s:Application>

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

12 thoughts on “Setting the vertical scroll position on a Spark List control in Flex 4

  1. I am missing the output :( but thanks anyway…

    Also, you might want to change the “warning” message at the beginning to Flash Builder Beta at Labs, It would be easier for people

  2. I have a question about this :)

    It does indeed work, if you have a simple list like this, but it doesn’t work when you add a skinclass to that list…
    Do you have any idea why? In the skinclass i just have a datagroup with an itemrenderer.

    I also tried putting a scroller around the datagroup (to have a viewport i guess) but that doesn’t work either …
    Could you please mail me some feedback?

    Thanks a lot!
    Maarten

  3. When I scroll to the max using the slider, I still can scroll using the scroll bar, why it doesn’t scroll to the max ?

  4. hi,
    i want to control a canvas scroll with d hslider.say,i have 12 date choosers inside a canvas in vertical layout.but i want to show three date choosers at one time as i scroll along.say the slider min val is 0 and max val is 1 with snap interval 0.25.so when i reach 0.25 i want to show first 3 date choosers,when i reach 0.5,then next three and so on..

Comments are closed.