Fading an item renderer background fill on a Spark List control in Flex 4

The following example shows how you can fade an item renderer background fill on a Spark List control in Flex 4 by creating a custom item renderer, setting the Boolean autoDrawBackground property to false, drawing your own background fill, and then setting a fade transition from the normal and hovered 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/06/25/fading-an-item-renderer-background-fill-on-a-spark-list-control-in-flex-4/ -->
<s:Application name="Spark_List_itemRenderer_fade_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.FadingItemRenderer"
            horizontalCenter="0" verticalCenter="0">
        <s:layout>
            <s:VerticalLayout gap="0"
                    horizontalAlign="contentJustify"
                    requestedRowCount="7" />
        </s:layout>
        <s:dataProvider>
            <s:ArrayList source="[One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten]" />
        </s:dataProvider>
    </s:List>
 
</s:Application>

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/06/25/fading-an-item-renderer-background-fill-on-a-spark-list-control-in-flex-4/ -->
<s:ItemRenderer name="FadingItemRenderer"
        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">
    <s:states>
        <s:State name="normal" />
        <s:State name="hovered" />
        <s:State name="selected" />
    </s:states>
    <s:transitions>
        <s:Transition fromState="normal" toState="hovered" autoReverse="true">
            <s:Fade target="{bgRect}" duration="600" />
        </s:Transition>
    </s:transitions>
 
    <s:Rect id="bgRect"
            includeIn="hovered,selected"
            left="0" right="0" top="0" bottom="0">
        <s:fill>
            <s:SolidColor color="{getStyle('rollOverColor')}"
                          color.selected="{getStyle('selectionColor')}" />
        </s:fill>
    </s:Rect>
 
    <s:Label id="labelDisplay"
            left="5" right="5" top="5" bottom="4" />
 
</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.