Using virtual layout on a Spark List control in Flex Gumbo

The following example shows how you can use virtual item layout on a Spark List control in Flex Gumbo by creating a custom skin and setting the useVirtualLayout property on the layout object.

Full code after the jump.

To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see “Using the beta Gumbo SDK in Flex Builder 3”.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/04/29/using-virtual-layout-on-a-spark-list-control-in-flex-gumbo/ -->
<s:Application name="Spark_List_layout_useVirtualLayout_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"
        initialize="init();">

    <fx:Script>
        <![CDATA[
            private function init():void {
                var idx:uint;
                var len:uint = 2000;
                var arr:Array = [];
                for (idx=0; idx<len; idx++) {
                    arr.push("Item #" + idx);
                }
                arrList = new ArrayList(arr);
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <s:ArrayList id="arrList" />
    </fx:Declarations>

    <s:List id="list"
            dataProvider="{arrList}"
            skinClass="skins.CustomListSkin"
            horizontalCenter="0"
            verticalCenter="0" />

</s:Application>

The custom Spark List skin, CustomListSkin.mxml, is as follows:

View skins/CustomListSkin.mxml

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/04/29/using-virtual-layout-on-a-spark-list-control-in-flex-gumbo/ -->
<s:SparkSkin name="CustomListSkin"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        minWidth="112" minHeight="112"
        alpha.disabled="0.5">
    <s:states>
        <s:State name="normal" />
        <s:State name="disabled" />
    </s:states>

    <fx:Metadata>
        <![CDATA[
            [HostComponent("spark.components.List")]
        ]]>
    </fx:Metadata>

    <fx:Script>
        /* Define the skin elements that should not be colorized.
           For list, the skin itself is colorized but the individual parts are not. */
        static private const exclusions:Array = ["scroller", "background"];

        override public function get colorizeExclusions():Array {return exclusions;}

        static private const contentFill:Array = ["bgFill"];
        override public function get contentItems():Array {return contentFill};
    </fx:Script>

    <!-- border -->
    <s:Rect left="0" right="0" top="0" bottom="0">
        <s:stroke>
            <s:SolidColorStroke color="0x686868" weight="1"/>
        </s:stroke>
    </s:Rect>

    <!-- fill -->
    <s:Rect id="background" left="1" right="1" top="1" bottom="1" >
        <s:fill>
            <s:SolidColor id="bgFill" color="0xFFFFFF" />
        </s:fill>
    </s:Rect>

    <s:Scroller id="scroller"
            focusEnabled="false"
            horizontalScrollPolicy="off"
            left="1" top="1" right="1" bottom="1">
        <s:DataGroup id="dataGroup"
                itemRenderer="spark.skins.default.DefaultItemRenderer">
            <s:layout>
                <s:VerticalLayout gap="0"
                        horizontalAlign="contentJustify"
                        useVirtualLayout="true"
                        requestedRowCount="6" />
            </s:layout>
        </s:DataGroup>
    </s:Scroller>

</s:SparkSkin>

View source is enabled in the following example.

This entry is based on a beta version of the Flex Gumbo 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 Gumbo SDK.