Using a custom item renderer with the Spark List control in Flex 4

The following example shows how you can specify a custom item renderer on the Flex 4 Spark List control by setting the itemRenderer property.

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/03/18/using-a-custom-item-renderer-with-the-fxlist-control-in-flex-gumbo/ -->
<s:Application name="Spark_List_itemRenderer_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">
 
    <fx:Declarations>
        <s:ArrayList id="arrList">
            <fx:Object firstName="Ryan"
                    lastName="Frishberg"
                    url="http://frishy.blogspot.com/" />
            <fx:Object firstName="Ely"
                    lastName="Greenfield"
                    url="http://www.quietlyscheming.com/blog/" />
            <fx:Object firstName="Chet"
                    lastName="Haase"
                    url="http://graphics-geek.blogspot.com/" />
            <fx:Object firstName="Alex"
                    lastName="Harui"
                    url="http://blogs.adobe.com/aharui/" />
            <fx:Object firstName="Joan"
                    lastName="Lafferty"
                    url="http://butterfliesandbugs.wordpress.com/" />
            <fx:Object firstName="Gordon"
                    lastName="Smith"
                    url="http://blogs.adobe.com/gosmith/" />
            <fx:Object firstName="Deepa"
                    lastName="Subramaniam"
                    url="http://iamdeepa.com/blog/" />
            <fx:Object firstName="Rob"
                    lastName="Vollmar"
                    url="http://flexspaghetti.wordpress.com/" />
        </s:ArrayList>
    </fx:Declarations>
 
    <s:List id="list"
            dataProvider="{arrList}"
            itemRenderer="skins.ResizingItemRenderer"
            width="250"
            horizontalCenter="0" verticalCenter="0" />
 
</s:Application>

And the custom item renderer, skins/ResizingItemRenderer.mxml, is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/18/using-a-custom-item-renderer-with-the-fxlist-control-in-flex-gumbo/ -->
<s:ItemRenderer name="ResizingItemRenderer"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        focusEnabled="false">
    <!-- states -->
    <s:states>
        <s:State name="normal" />
        <s:State name="hovered" />
        <s:State name="selected" />
        <s:State name="normalAndShowsCaret"/>
        <s:State name="hoveredAndShowsCaret"/>
        <s:State name="selectedAndShowsCaret"/>
    </s:states>
 
    <s:Rect left="0" right="0" top="0" bottom="0">
        <s:stroke.normalAndShowsCaret>
            <s:SolidColorStroke color="{selectionColor}" weight="1"/>
        </s:stroke.normalAndShowsCaret>
        <s:stroke.hoveredAndShowsCaret>
            <s:SolidColorStroke color="{selectionColor}" weight="1"/>
        </s:stroke.hoveredAndShowsCaret>
        <s:stroke.selectedAndShowsCaret>
            <s:SolidColorStroke color="{selectionColor}" weight="1"/>
        </s:stroke.selectedAndShowsCaret>
        <s:fill>
            <s:SolidColor color.normal="{contentBackgroundColor}"
                    color.normalAndShowsCaret="{contentBackgroundColor}"
                    color.hovered="{rollOverColor}"	
                    color.hoveredAndShowsCaret="{rollOverColor}"
                    color.selected="{selectionColor}"
                    color.selectedAndShowsCaret="{selectionColor}" />
        </s:fill>
    </s:Rect>
 
    <s:VGroup left="3" right="10" top="3" bottom="3">
        <s:SimpleText text="{data.firstName} {data.lastName}" fontWeight="bold" />
        <s:RichEditableText editable="false"
                width="100%"
                includeIn="selected, selectedAndShowsCaret">
            <s:content>
                <s:p><s:a href="{data.url}">{data.url}</s:a></s:p>
            </s:content>
        </s:RichEditableText>
    </s:VGroup>
 
</s:ItemRenderer>

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.

One thought on “Using a custom item renderer with the Spark List control in Flex 4

  1. Looks good, i have a question:
    Lets say i have lots of items so i have a scrollbar, and i click the last item in the list.
    Then the row where the links are is not visible on the screen. Any thoughts how to solve this?

Comments are closed.