Toggling horizontal and vertical scroll bars on a Spark List control in Flex 4

The following example shows how you can show or hide horizontal and vertical scroll bars on a Spark List control in Flex 4 by setting the horizontalScrollPolicy and/or verticalScrollPolicy styles.

Full code after the jump.

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/10/05/toggling-horizontal-and-vertical-scroll-bars-on-a-spark-list-control-in-flex-4/ -->
<s:Application name="Spark_List_scroller_horizontalScrollPolicy_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
    <s:controlBarContent>
        <mx:Form styleName="plain" backgroundAlpha="0.0">
            <mx:FormItem label="horizontalScrollPolicy:">
                <s:ButtonBar id="hSP" requireSelection="true" change="hSP_changeHandler(event);">
                    <s:dataProvider>
                        <s:ArrayList source="[auto,off,on]" />
                    </s:dataProvider>
                </s:ButtonBar>
            </mx:FormItem>
            <mx:FormItem label="verticalScrollPolicy:">
                <s:ButtonBar id="vSP" requireSelection="true" change="vSP_changeHandler(event);">
                    <s:dataProvider>
                        <s:ArrayList source="[auto,off,on]" />
                    </s:dataProvider>
                </s:ButtonBar>
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import spark.events.IndexChangeEvent;
 
            protected function hSP_changeHandler(evt:IndexChangeEvent):void {
                lst.scroller.setStyle("horizontalScrollPolicy", hSP.selectedItem);
            }
 
            protected function vSP_changeHandler(evt:IndexChangeEvent):void {
                lst.scroller.setStyle("verticalScrollPolicy", vSP.selectedItem);
            }
        ]]>
    </fx:Script>
 
    <s:List id="lst"
            horizontalCenter="0" verticalCenter="0">
        <s:layout>
            <s:VerticalLayout gap="0"
                    horizontalAlign="contentJustify"
                    requestedRowCount="5" />
        </s:layout>
        <s:dataProvider>
            <s:ArrayList>
                <fx:Object label="1. One" />
                <fx:Object label="2. Two" />
                <fx:Object label="3. Three" />
                <fx:Object label="4. Four" />
                <fx:Object label="5. Five" />
                <fx:Object label="6. Six" />
                <fx:Object label="7. Seven" />
                <fx:Object label="8. Eight" />
                <fx:Object label="9. Nine" />
                <fx:Object label="10. The quick brown fox jumps over the lazy dog" />
            </s:ArrayList>
        </s:dataProvider>
    </s:List>
 
</s:Application>

You can also set the horizontalScrollPolicy and verticalScrollPolicy using advanced CSS, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/10/05/toggling-horizontal-and-vertical-scroll-bars-on-a-spark-list-control-in-flex-4/ -->
<s:Application name="Spark_List_scroller_horizontalScrollPolicy_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/halo";
 
        s|List#lst s|Scroller {
            horizontalScrollPolicy: off;
            verticalScrollPolicy: off;
        }
    </fx:Style>
 
    <s:List id="lst"
            horizontalCenter="0" verticalCenter="0">
        <s:layout>
            <s:VerticalLayout gap="0"
                    horizontalAlign="contentJustify"
                    requestedRowCount="5" />
        </s:layout>
        <s:dataProvider>
            <s:ArrayList>
                <fx:Object label="1. One" />
                <fx:Object label="2. Two" />
                <fx:Object label="3. Three" />
                <fx:Object label="4. Four" />
                <fx:Object label="5. Five" />
                <fx:Object label="6. Six" />
                <fx:Object label="7. Seven" />
                <fx:Object label="8. Eight" />
                <fx:Object label="9. Nine" />
                <fx:Object label="10. The quick brown fox jumps over the lazy dog" />
            </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.