Creating an item renderer with a corner radius on a Spark List control in Flex 4

The following example shows how you can create a custom item renderer with a corner radius on a Spark List control in Flex 4 by setting the cornerRadius property and creating a custom item renderer which sets its autoDrawBackground property to false and draws its own rounded background fill using the radiusX property in the hovered and selected states.

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/15/creating-an-item-renderer-with-a-corner-radius-on-a-spark-list-control-in-flex-4/ -->
<s:Application name="Spark_List_itemRenderer_radiusX_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.CustomCornerRadiusItemRenderer"
            allowMultipleSelection="true"
            requireSelection="true"
            horizontalCenter="0" verticalCenter="0">
        <s:layout>
            <s:TileLayout requestedColumnCount="3" requestedRowCount="3"
                    columnWidth="80" rowHeight="60"
                    horizontalGap="0" verticalGap="0" />
        </s:layout>
        <s:dataProvider>
            <s:ArrayList>
                <fx:Object label="One" />
                <fx:Object label="Two" />
                <fx:Object label="Three" />
                <fx:Object label="Four" />
                <fx:Object label="Five" />
                <fx:Object label="Six" />
                <fx:Object label="Seven" />
                <fx:Object label="Eight" />
                <fx:Object label="Nine" />
                <fx:Object label="Ten" />
            </s:ArrayList>
        </s:dataProvider>
    </s:List>
 
</s:Application>

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/04/15/creating-an-item-renderer-with-a-corner-radius-on-a-spark-list-control-in-flex-4/ -->
<s:ItemRenderer name="CustomCornerRadiusItemRenderer"
        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="false">
    <!-- states -->
    <s:states>
        <s:State name="normal" />
        <s:State name="hovered" />
        <s:State name="selected" />
    </s:states>
 
    <fx:Script>
        <![CDATA[
            override public function set data(value:Object):void {
                labelDisplay.text = value.label;
            }
        ]]>
    </fx:Script>
 
    <s:Rect id="bgFill"
            radiusX="10"
            includeIn="hovered,selected"
            left="2" right="2" top="2" bottom="2">
        <s:fill>
            <s:SolidColor color="haloSilver"
                    color.selected="haloBlue"
                    alpha="0.6" />
        </s:fill>
    </s:Rect>
 
    <s:Label id="labelDisplay"
            left="10" right="10" top="10" bottom="10" />
 
</s:ItemRenderer>

[GoogleAdsWide]

View source is enabled in the following example.

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.