23 May 2011
Beginning
In this exercise, you will create a custom item renderer and use it to display the employee data as shown in Figure 1.
In this exercise, you will learn how to:
In this section, you will use MXML to create a custom item renderer for a DataGroup container.
Declarations comment, locate the Declarations block.Declarations block, note the ArrayList tags and nested Object tags. This represents the data that is being used as the data provider.Note: You are using an ArrayList instance instead of an ArrayCollection instance for this example since the data will not be updated, and therefore, does not need the overhead of the bindings in the ArrayCollection class.
<s:ArrayList id="employeeList">
<fx:Object firstName="Samuel" lastName="Ang"
imageFile="images/sang.jpg"/>
<fx:Object firstName="Athena" lastName="Parker"
imageFile="images/aparker.jpg"/>
<fx:Object firstName="Saul"
lastName="Tucker"
imageFile="images/stucker.jpg"/>
<fx:Object firstName="Alyssa" lastName="Le"
imageFile="images/ale.jpg"/>
</s:ArrayList>
DataGroup component.DataGroup component tags, below the layout property tags, create an itemRenderer property tag set.<s:DataGroup dataProvider="{employeeList}">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:itemRenderer>
</s:itemRenderer>
</s:DataGroup>
itemRenderer property tags, create a set of Component tags.<s:itemRenderer>
<fx:Component>
</fx:Component>
</s:itemRenderer>
Component block, create an ItemRenderer class block.<s:itemRenderer>
<fx:Component>
<s:ItemRenderer>
</s:ItemRenderer>
</fx:Component>
</s:itemRenderer>
Remember that itemRenderer (lowercase i) is a property of the DataGroup class while ItemRenderer (capital I) is a class that you are instantiating.
ItemRenderer tags, create a states tag block.<fx:Component>
<s:ItemRenderer>
<s:states>
</s:states>
</s:ItemRenderer>
</fx:Component>
states block, create a State instance and assign normal as the name. <s:states>
<s:State name="normal"/>
</s:states>
ItemRenderer tags, below the states tag block, create an BorderContainer block. <s:ItemRenderer>
<s:states>
<s:State name="normal"/>
</s:states>
<s:BorderContainer>
</s:BorderContainer>
</s:ItemRenderer>
BorderContainer tag, add the borderWeight property with a value of 2, the backgroundColor property with a value of #cccccc, the top property with a value of 5 and the height and width properties with a values of 100%.<s:BorderContainer borderWeight="2"
backgroundColor="#cccccc"
top="5" height="100%" width="100%">
</s:BorderContainer>
BorderContainer block, create a layout block.<s:BorderContainer borderWeight="2"
backgroundColor="#cccccc"
top="5" height="100%" width="100%">
<s:layout>
</s:layout>
</s:BorderContainer>
layout block, add the VerticalLayout tag. <s:BorderContainer borderWeight="2"
backgroundColor="#cccccc"
top="5" height="100%" width="100%">
<s:layout>
<s:VerticalLayout/>
</s:layout>
</s:BorderContainer>
BorderContainer block, below the layout block, create an Label control.<s:BorderContainer borderWeight="2"
backgroundColor="#cccccc"
top="5" height="100%" width="100%">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Label />
</s:BorderContainer>
Label control, add the text property and bind data.firstName and data.lastName as the value.<s:Label text="{data.firstName} {data.lastName}"/>
Data for the current object that is being evaluated is referenced in the data property of the item renderer.
BorderContainer block, below the Label control, create an BitmapImage control....
<s:Label text="{data.firstName} {data.lastName}"/>
<s:BitmapImage />
</s:BorderContainer>
BitmapImage control, add the source property with a bound value of data.imageFile.<s:BitmapImage source="{data.imageFile}"/>
You should see the employee images and names displayed with a border and background color, as shown in Figure 2. Note, however that the employees are not centered within the item renderer.
BorderContainer block.layout block, to the VerticalLayout tag, add the horizontalAlign property with a value of center.<s:BorderContainer borderWeight="2" backgroundColor="#cccccc"
top="5" height="100%" width="100%">
<s:layout>
<s:VerticalLayout horizontalAlign="center"/>
</s:layout>
<s:Label text="{data.firstName} {data.lastName}"/>
<s:BitmapImage source="{data.imageFile}"/>
</s:BorderContainer>
You should see the employee name and image are now centered horizontally in each item renderer (see Figure 3).
BorderContainer block's layout block, to the VerticalLayout tag, add the verticalAlign property with a value of middle.<s:BorderContainer borderWeight="2" backgroundColor="#cccccc"
top="5"height="100%" width="100%">
<s:layout>
<s:VerticalLayout horizontalAlign="center"
verticalAlign="middle"/>
</s:layout>
<s:Label text="{data.firstName} {data.lastName}"/>
<s:BitmapImage source="{data.imageFile}"/>
</s:BorderContainer>
You should see the employee name and image are now vertically and horizontally centered in the item renderer (see Figure 4).
Your New Item Renderer dialog box should appear as shown in Figure 6.
The EmployeeItemRenderer.mxml file will open in the editor.
Label control that was generated with the file.DataGroup component.DataGroup component, cut the code between the ItemRenderer tags.ItemRenderer tags..DataGroup component, delete the itemRenderer block and its contents.DataGroup component, add the itemRenderer property with a value of components.EmployeeItemRenderer.<s:DataGroup dataProvider="{employeeList}"
itemRenderer="components.EmployeeItemRenderer">
<s:layout>
<s:VerticalLayout/>
</s:layout>
</s:DataGroup>
You should see the application appears as it did before, as shown in Figure 7.
In this exercise, you learned how to create a custom item renderer component for a group of data. In the next exercise you will apply the item renderer component created in this exercise to a Spark List control.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.