Creating a variable row height Spark DropDownList control in Flex 4

In a previous example, “Creating a variable row height item renderer on a ComboBox control in Flex”, we saw how you could create a variable row height item renderer on a Flex MX ComboBox control by setting the variableRowHeight property on the ComboBox control’s dropdown property.

The following example shows how you can create a variable row height Spark DropDownList control in Flex 4 by setting a custom VerticalLayout object and setting the Boolean variableRowHeight property and the horizontalAlign 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/2010/01/19/creating-a-variable-row-height-spark-dropdownlist-control-in-flex-4/ -->
<s:Application name="Spark_DropDownList_layout_variableRowHeight_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:DropDownList id="ddl"
            requireSelection="true"
            labelField="name"
            alternatingItemColors="[#FFFFFF,#EEEEEE]"
            horizontalCenter="0"
            top="20"
            width="100">
        <s:layout>
            <s:VerticalLayout gap="0"
                    variableRowHeight="true"
                    horizontalAlign="justify" />
        </s:layout>
        <s:dataProvider>
            <s:ArrayList>
                <fx:Array id="arr">
                    <fx:Object name="Baltimore Orioles" abbr="BAL" />
                    <fx:Object name="Boston Red Sox" abbr="BOS" />
                    <fx:Object name="Chicago White Sox" abbr="CWS" />
                    <fx:Object name="Cleveland Indians" abbr="CLE" />
                    <fx:Object name="Detroit Tigers" abbr="DET" />
                    <fx:Object name="Kansas City Royals" abbr="KC" />
                    <fx:Object name="Los Angeles Angels of Anaheim" abbr="LAA" />
                    <fx:Object name="Minnesota Twins" abbr="MIN" />
                    <fx:Object name="New York Yankees" abbr="NYY" />
                    <fx:Object name="Oakland Athletics" abbr="OAK" />
                    <fx:Object name="Seattle Mariners" abbr="SEA" />
                    <fx:Object name="Tampa Bay Devil Rays" abbr="TB" />
                    <fx:Object name="Texas Rangers" abbr="TEX" />
                    <fx:Object name="Toronto Blue Jays" abbr="TOR" />
                </fx:Array>
            </s:ArrayList>
        </s:dataProvider>
    </s:DropDownList>
 
</s:Application>

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.