Displaying images in a Spark List control in Flex 4

The following example shows how you can display images automatically in a Spark List control in Flex 4 by setting the itemRenderer property to the DefaultComplexItemRenderer class.

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/09/18/displaying-images-in-a-spark-list-control-in-flex-4/ -->
<s:Application name="Spark_List_itemRenderer_DefaultComplexItemRenderer_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:List id="list"
            itemRenderer="spark.skins.spark.DefaultComplexItemRenderer"
            horizontalCenter="0" verticalCenter="0">
        <s:layout>
            <s:HorizontalLayout requestedColumnCount="3" gap="0" />
        </s:layout>
        <s:dataProvider>
            <s:ArrayList>
                <s:BitmapImage source="@Embed('ai_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('air_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('cf_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('dw_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('fl_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('fl_player_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('fw_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('fx_appicon-tn.gif')" />
            </s:ArrayList>
        </s:dataProvider>
    </s:List>
 
</s:Application>

You can also declare the itemRenderer property using MXML, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/09/18/displaying-images-in-a-spark-list-control-in-flex-4/ -->
<s:Application name="Spark_List_itemRenderer_DefaultComplexItemRenderer_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"
        xmlns:skins="spark.skins.spark.*">
 
    <s:List id="list"
            horizontalCenter="0" verticalCenter="0">
        <s:layout>
            <s:HorizontalLayout requestedColumnCount="3" gap="0" />
        </s:layout>
        <s:itemRenderer>
            <fx:Component>
                <skins:DefaultComplexItemRenderer />
            </fx:Component>
        </s:itemRenderer>
        <s:dataProvider>
            <s:ArrayList>
                <s:BitmapImage source="@Embed('ai_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('air_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('cf_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('dw_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('fl_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('fl_player_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('fw_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('fx_appicon-tn.gif')" />
            </s:ArrayList>
        </s:dataProvider>
    </s:List>
 
</s:Application>

Or you can set the layout and itemRenderer properties in a custom skin file, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/09/18/displaying-images-in-a-spark-list-control-in-flex-4/ -->
<s:Application name="Spark_List_itemRenderer_DefaultComplexItemRenderer_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:List id="list"
            skinClass="skins.CustomListSkin"
            horizontalCenter="0" verticalCenter="0">
        <s:dataProvider>
            <s:ArrayList>
                <s:BitmapImage source="@Embed('ai_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('air_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('cf_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('dw_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('fl_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('fl_player_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('fw_appicon-tn.gif')" />
                <s:BitmapImage source="@Embed('fx_appicon-tn.gif')" />
            </s:ArrayList>
        </s:dataProvider>
    </s:List>
 
</s:Application>

And the custom skin class, skins/CustomListSkin.mxml, is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/09/18/displaying-images-in-a-spark-list-control-in-flex-4/ -->
<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"
        blendMode="normal">
    <s:states>
        <s:State name="normal" />
        <s:State name="disabled" />
    </s:states>
 
    <fx:Metadata>
        [HostComponent("spark.components.List")]
    </fx:Metadata>
 
    <fx:Script>
        <![CDATA[
            /* 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;
            }
 
            /* Define the content fill items that should be colored by the "contentBackgroundColor" style. */
            static private const contentFill:Array = ["bgFill"];
 
            override public function get contentItems():Array {
                return contentFill
            };
 
            override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
                if (getStyle("borderVisible") == true) {
                    border.visible = true;
                    background.left = background.top = background.right = background.bottom = 1;
                    scroller.minViewportInset = 1;
                } else {
                    border.visible = false;
                    background.left = background.top = background.right = background.bottom = 0;
                    scroller.minViewportInset = 0;
                }
                super.updateDisplayList(unscaledWidth, unscaledHeight);
            }
        ]]>
    </fx:Script>
 
    <!-- border -->
    <s:Rect id="border" left="0" right="0" top="0" bottom="0">
        <s:stroke>
            <s:SolidColorStroke color="{getStyle('borderColor')}"
                                alpha="{getStyle('borderAlpha')}"
                                weight="1"/>
        </s:stroke>
    </s:Rect>
 
    <!-- fill -->
    <!--- Defines the background appearance of the list-based component. -->
    <s:Rect id="background" left="1" right="1" top="1" bottom="1" >
        <s:fill>
            <!--- Defines the color of the background. The default color is 0xFFFFFF. -->
            <s:SolidColor id="bgFill" color="0xFFFFFF" />
        </s:fill>
    </s:Rect>
 
    <!--- The Scroller component to add scroll bars to the list. -->
    <s:Scroller id="scroller" left="0" top="0" right="0" bottom="0" minViewportInset="1" focusEnabled="false">
        <!--- The container for the data items. -->
        <s:DataGroup id="dataGroup" itemRenderer="spark.skins.spark.DefaultComplexItemRenderer">
            <s:layout>
                <s:HorizontalLayout requestedColumnCount="3" gap="0" />
            </s:layout>
        </s:DataGroup>
    </s:Scroller>
 
</s:SparkSkin>

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.

2 thoughts on “Displaying images in a Spark List control in Flex 4

Comments are closed.