Displaying icons in a Spark List control in Flex 4

The following example shows how you can display icons in a Spark List control in Flex 4 by creating a custom item renderer and setting the itemRenderer property.

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/2010/04/29/displaying-icons-in-a-spark-list-control-in-flex-4-2/ -->
<s:Application name="Spark_List_itemRenderer_icon_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:List id="lst"
            itemRenderer="skins.StarredItemRenderer"
            width="100"
            horizontalCenter="0" verticalCenter="0">
        <s:layout>
            <s:VerticalLayout gap="0" horizontalAlign="justify" />
        </s:layout>
        <s:dataProvider>
            <s:ArrayList>
                <fx:Object label="Red" />
                <fx:Object label="Orange" starred="true" />
                <fx:Object label="Yellow" starred="false" />
                <fx:Object label="Green" starred="true" />
                <fx:Object label="Blue" starred="true" />
                <fx:Object label="The quick brown fox jumps over the lazy dog" />
            </s:ArrayList>
        </s:dataProvider>
    </s:List>
 
</s:Application>

And the custom Spark List control item renderer, skins/StarredItemRenderer.mxml, is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/04/29/displaying-icons-in-a-spark-list-control-in-flex-4-2/ -->
<s:ItemRenderer name="StarredItemRenderer"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx"
        autoDrawBackground="true">
 
    <fx:Script>
        <![CDATA[
            override public function set data(obj:Object):void {
                if (obj.hasOwnProperty("starred") && (obj['starred'] == true)) {
                    star.visible = true;
                }
            }
        ]]>
    </fx:Script>
 
    <s:HGroup verticalAlign="middle" left="3" right="3" top="3" bottom="3" gap="2">
        <s:BitmapImage id="star" source="@Embed('assets/bullet_star.png')" visible="false" />
        <s:Label id="labelDisplay" maxDisplayedLines="1" width="100%" />
    </s:HGroup>
 
</s:ItemRenderer>

[Famfamfam]

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.