28 October 2008
General experience with programming in ActionScript 3 is suggested.
Intermediate
The ActionScript 3.0 TileList component presents images or other display objects as tiles that are arranged in rows and columns. A tile is the intersection of a row and a column and is also commonly called a cell in list-based components. You can specify the number of rows and columns that are visible in the tile list and scroll either vertically or horizontally to view content that extends beyond its borders.
A TileList object uses a cell renderer class to display each tile. The default cell renderer for the TileList component is the ImageCell class, which displays a thumbnail image and a single-line text label. The following example represents a typical TileList application.
To get the source files for this example, download TileList_basic.zip from the top of this page.
This article covers the following topics: adding the TileList component to your application, using a data provider to add content to a TileList component, merging and sorting TileList data providers, calculating TileList cell layout, customizing the TileList component using styles, and customizing tile appearance using a custom cell renderer.
You can add an instance of the TileList component to your application either at authoring time using Flash or at runtime by using ActionScript 3.0. The following procedures show how to add a tile list containing four images to a Flash application.
This example uses the addItem() method, which TileList inherits from the SelectableList class, to add content to the TileList instance. By default, the label property of an item is used to display the label of the item and the source property holds the data for the item.
direction parameter to vertical.columnCount parameter to 1.columnWidth parameter to 200.rowCount parameter to 1.rowHeight parameter to 140. myTileList.addItem({label:"Montreal", source:"http://www.helpexamples.com/images/montreal/images/IMG_5057.jpg"});
myTileList.addItem({label:"Swan", source:"http://www.helpexamples.com/flash/images/gallery1/images/pic11.jpg"});
myTileList.addItem({label:"Sunset", source:"http://www.helpexamples.com/flash/images/gallery1/images/pic14.jpg"});
myTileList.addItem({label:"Gate", source:"http://www.helpexamples.com/flash/images/gallery2/images/IMG_1592.jpg"});
The following procedure uses ActionScript to add a TileList instance to the application at runtime. It also dynamically sets the position of the tile list, the column width and row height, and the number of columns and rows that are visible.
// import required classes
import fl.controls.TileList;
import fl.controls.ScrollBarDirection;
// Create TileList instance
var myTileList:TileList = new TileList();
// Add four images to the TileList instance
myTileList.addItem({label:"Montreal", source:"http://www.helpexamples.com/images/montreal/images/IMG_5057.jpg"});
myTileList.addItem({label:"Swan", source:"http://www.helpexamples.com/flash/images/gallery1/images/pic11.jpg"});
myTileList.addItem({label:"Sunset", source:"http://www.helpexamples.com/flash/images/gallery1/images/pic14.jpg"});
myTileList.addItem({label:"Gate", source:"http://www.helpexamples.com/flash/images/gallery2/images/IMG_1592.jpg"});
// Set scroll bar direction
myTileList.direction = ScrollBarDirection.HORIZONTAL;
// position TileList and set column and row values
myTileList.move(0,0);
myTileList.columnWidth = 200;
myTileList.rowHeight = 140;
myTileList.columnCount = 1;
myTileList.rowCount = 2;
myTileList.width = 200;
myTileList.height = 295;
// Add to the display (Stage)
addChild(myTileList);
To get the source files for this example, download TileList_AS.zip at the top of this page.
In addition to using the TileList addItem() method, you can add content to a TileList by setting its dataProvider property. The property accepts a DataProvider object that represents the data model of the items to be viewed in the tile list. A data provider is a data source that is typically a linear collection of data such as an Array or an XML object. Using a DataProvider object is a more powerful way to add content to a tile list because its API also allows you to access, remove, merge, and sort items.
The easiest way to create a data provider for the TileList component is by assigning values to the dataProvider parameter in the Component inspector. When you double-click the dataProvider parameter, Flash displays the Values collection dialog box.
For each item that you want to assign to the TileList, click the Plus sign (+) to add a new item and enter values for the label and the source parameters. The source value specifies the content that you want to appear in a tile and the label allows you to add a text label to it. Click the Minus sign (–) to delete an item or click the up or down arrows to move items up or down in the list. The following example adds to the tile list the same items that we saw in the preceding examples.
Another way to create a DataProvider object is with ActionScript. Use the new operator with the DataProvider class constructor function, passing the data source as an argument. The data source can be an array or an XML object that contains the data items. The following example creates a DataProvider using an array as the data source:
import fl.data.DataProvider;
import fl.controls.ScrollBarDirection;
var pics:Array = [
{label: "Image 1", source: "http://www.helpexamples.com/images/montreal/images/IMG_5057.jpg"},
{label: "Image 2", source: "http://www.helpexamples.com/flash/images/gallery1/images/pic11.jpg"},
{label: "Image 3", source: "http://www.helpexamples.com/flash/images/gallery1/images/pic14.jpg"},
{label: "Image 4", source: "http://www.helpexamples.com/flash/images/gallery2/images/IMG_1592.jpg"}];
// set column and row values and position TileList
myTileList.dataProvider = new DataProvider(pics);
myTileList.direction = ScrollBarDirection.VERTICAL;
myTileList.rowHeight = 140;
myTileList.columnWidth = 200;
myTileList.columnCount = 2;
myTileList.rowCount = 1;
myTileList.width = 415;
myTileList.move(0,0);
The following ActionScript code supplies the same content in an ActionScript 3.0 XML object instead of an array. It also sets the scroll direction to horizontal and reverses the columnCount and rowCount values.
import fl.data.DataProvider;
import fl.controls.ScrollBarDirection
var picsXML:XML = <items>
<item label="Image 1" source="http://www.helpexamples.com/images/montreal/images/IMG_5057.jpg" />
<item label="Image 2" source="http://www.helpexamples.com/flash/images/gallery1/images/pic11.jpg" />
<item label="Image 3" source="http://www.helpexamples.com/flash/images/gallery1/images/pic14.jpg" />
<item label="Image 4" source="http://www.helpexamples.com/flash/images/gallery2/images/IMG_1592.jpg" />
</items>;
// set column and row values and position TileList
myTileList.dataProvider = new DataProvider(picsXML);
myTileList.direction = ScrollBarDirection.HORIZONTAL;
myTileList.rowHeight = 140;
myTileList.columnWidth = 200;
myTileList.columnCount = 1;
myTileList.rowCount = 2;
myTileList.width = 200;
myTileList.height = 295;
myTileList.move(10,10);
To get the source files for this example, download TileList_XML.zip at the top of this page.
The following example adds movie clips to a tile list and shows how to merge and sort data providers as well as how to remove items using the removeAll() method. It uses two DataProvider objects—one for boxes (boxesDp) and one for circles (circlesDp)—to add content to a TileList instance. The example also includes a button that provides for user interaction. Initially, the example calls the loadBoxes() and loadCircles() methods to load the two data providers and fill the tile list with an array of colored boxes. It also sets the button's label to "Merge Circles" and creates an event handler (buttonClickHandler()) that is invoked when the user clicks the button.
When the user clicks the Merge Circles button the buttonClickHandler() method does the following:
circlesDp DataProviderWhen the user clicks the Sort Descending Button, the buttonClickHandler() method sorts the boxes and circles in descending order by the value of the label and sets the Button's label to "Restore".
When the user clicks the Restore button the buttonClickHandler() method does the following:
removeAll() method to delete the contents of the tile list loadBoxes() method to restore the initial content to the tile listFollow these steps to implement the example.
label parameter to Merge Circles.import fl.data.DataProvider;
import flash.display.Shape;
// Create arrays for boxes and circles
var boxArray:Array = new Array();
var circleArray:Array = new Array();
// Array of colors for shapes
var colors:Array = new Array(0x00000, 0xFF0000, 0x0000CC, 0x00CC00, 0xFFFF00);
var i:uint = 0;
// Create 2 DataProviders - 1 for boxes, 1 for circles
var boxesDp:DataProvider = new DataProvider();
var circlesDp:DataProvider = new DataProvider();
// Add boxes
loadBoxes();
// Load dpCircles
loadCircles();
// Add handler for Button clicks
mergeButton.addEventListener(MouseEvent.CLICK, buttonClickHandler);
function loadBoxes():void
{
// Array of color names for box labels
var boxColorNames:Array = new Array("Midnight", "Crimson", "Sky", "Forest", "July");
for(i=0; i < colors.length; i++)
{
boxArray[i] = new Shape();
// Draw box w next color in array
drawBox(boxArray[i], colors[i]);
// Add label and box to boxesDp
boxesDp.addItem( {label:boxColorNames[i], source:boxArray[i]} )
}
myTileList.dataProvider = boxesDp;
myTileList.columnWidth = 110;
myTileList.rowHeight = 110;
myTileList.setSize(560,230);
myTileList.rowCount = 2;
myTileList.move(10, 10);
myTileList.setStyle("contentPadding", 5);
}
// Draw a box using the color that was passed
function drawBox(box:Shape,color:uint):void
{
box.graphics.beginFill(color, 1.0);
box.graphics.drawRect(0, 0, 100, 100);
box.graphics.endFill();
}
function loadCircles():void
{
// Array of color names for circle labels
var circleColorNames:Array = new Array("Tar", "Raspberry", "Frost", "Apple", "Daylight");
for(i=0; i < colors.length; i++)
{
circleArray[i] = new Shape();
// Draw circle w next color in array
drawCircle(circleArray[i], colors[i]);
// Add label and circle to circlesDp
circlesDp.addItem( {label:circleColorNames[i], source:circleArray[i]} );
}
}
// Draw a circle using the color that was passed
function drawCircle(circle:Shape,color:uint):void
{
circle.graphics.beginFill(color, 1.0);
circle.graphics.drawCircle(75, 75, 75);
circle.graphics.endFill();
}
// Event handler for Button clicks
function buttonClickHandler(event:MouseEvent):void
{
// Merge circles
if(mergeButton.label == "Merge Circles")
{
myTileList.dataProvider.merge(circlesDp);
myTileList.dataProvider.sortOn("label"); // sort ascending by label
// Change Button label
mergeButton.label = "Sort Descending";
return;
}
// Sort descending
if(mergeButton.label == "Sort Descending")
{
myTileList.dataProvider.sortOn("label", Array.DESCENDING);
// Change Button label
mergeButton.label = "Restore";
return;
}
if(mergeButton.label == "Restore")
{
// Delete current contents
myTileList.dataProvider.removeAll();
// Load boxes
loadBoxes();
// Change Button label
mergeButton.label = "Merge Circles";
}
}
To get the source files for this example, download TileList_merge.zip from the top of this page.
The TileList component provides the following properties that affect the layout of columns and rows and the size of the TileList: columnCount, columnWidth, direction, rowCount, rowWidth and the height and width properties inherited from the UIComponent class. In addition, you can also use the contentPadding style to add padding to the perimeter of the TileList instance. The contentPadding style is described in more detail in the following section on using styles.
To ensure that the tile list's cells lay out the way you want them, Adobe makes the following recommendations:
columnCount and rowCount properties after you set the columnWidth, rowHeight, and dataProvider properties. The columnCount and rowCount values initially affect the width and height of the component but the component does not maintain them. If they are set with the Property inspector, they are maintained until the component is first drawn.height and width values for the component are in agreement with the columnCount, columnWidth, rowCount, rowHeight, and contentPadding valuesIn other words, the width that you specify for the component should equal the value of columnCount times the value of columnWidth, plus 2 times the value of contentPadding, plus 15 pixels for the width of the scroll bar if the direction property is vertical, causing the scroll bar to appear on the right side of the tile list. Likewise, the height of the tile list should equal the value of rowCount times the value of rowHeight, plus 2 times the value ofcontentPadding, plus 15 pixels for the width of the scroll bar if the direction is horizontal. When the direction is horizontal, the scroll bar appears at the bottom of the TileList. You can use the following formulas to calculate the values for the width and height properties:
width = columnWidth × columnCount + (contentPadding × 2) + 15 if scroll bar direction is vertical
height = rowHeight × rowCount + (contentPadding × 2) + 15 if scroll bar direction is horizontal
For example, the property values shown in the following table would result in the width and height values shown in the last two rows.
columnWidth
|
200
|
60
|
75
|
|---|---|---|---|
columnCount
|
2
|
3
|
4
|
scroll bar direction (+15)
|
vertical
|
horizontal
|
None
|
rowHeight
|
75
|
100
|
100
|
rowCount
|
10
|
3
|
6
|
contentPadding
|
3 (times 2)
|
None
|
2 (times 2)
|
= width
|
421
|
180
|
304
|
= height
|
756
|
315
|
604
|
Note: The contentPadding value affects both the top and bottom and the right and the left sides of the tile list so you must multiply its value by 2 in calculating the width and the height. Also, the scroll bar only takes effect if the contents exceed the boundaries of the tile list. If the contents will not exceed the boundaries of the tile list, you do not need to account for the 15-pixel width of the scroll bar.
You can customize cell content by setting styles for the tile list. For example, you can change the size and appearance of the labels or assign a value to contentPadding styles to create a border around the content. You can add custom content to the TileList either by extending the CellRenderer class or by implementing the ICellRenderer interface to create a custom cell renderer class. For example, you might want to display a different type of data or alter the appearance of the skins.
The following example creates custom labels and adds padding for the images in a tile list. It creates a TextFormat object and sets its font, color, bold, and size properties to specify the textFormat style for the TileList labels. The example uses the setRendererStyle() method to apply the textFormat style to the tiles, but not to the overall TileList. The setStyle() method sets a text format for the TileList component and all of its tiles; the setRendererStyle() method overrides the text format for the tiles themselves.
The example also sets the contentPadding and imagePadding styles. The contentPadding style is a TileList style that is set with the setStyle() method. It adds a border of the specified number of pixels to the perimeter of the tile list. The imagePadding style is an ImageCell style. The ImageCell class is the default cell renderer for the TileList component. The imagePadding style creates white space of the specified number of pixels around each image in the tile list. This setting reduces the size of the images and does not affect the dimensions of the TileList instance.
import fl.controls.TileList;
import fl.controls.ScrollBarDirection
// create TileList instance
var myTileList:TileList = new TileList();
// create TextFormat object for labels
var tf:TextFormat = new TextFormat();
//set textFormat font, color, and size
tf.font = "Verdana";
tf.bold = true;
tf.color = 0x990000;
tf.size = 16;
// add content to the TileList
myTileList.addItem({label:"Montreal", source:"http://www.helpexamples.com/images/montreal/images/IMG_5057.jpg"});
myTileList.addItem({label:"Swan", source:"http://www.helpexamples.com/flash/images/gallery1/images/pic11.jpg"});
myTileList.addItem({label:"Sunset", source:"http://www.helpexamples.com/flash/images/gallery1/images/pic14.jpg"});
myTileList.addItem({label:"Gate", source:"http://www.helpexamples.com/flash/images/gallery2/images/IMG_1592.jpg"});
myTileList.direction = ScrollBarDirection.HORIZONTAL; // set scroll direction
// set column and row values
myTileList.columnWidth = 200;
myTileList.rowHeight = 140;
myTileList.columnCount = 1;
myTileList.rowCount = 2;
myTileList.width = 210;
myTileList.height = 305;
//set styles
myTileList.setStyle("contentPadding", 5);
myTileList.setRendererStyle("textFormat", tf);
myTileList.setRendererStyle("imagePadding", 10);
// add to the display (Stage)
addChild(myTileList);
myTileList.move(5,5);
To get the source files for this example, download TileList_style.zip from the top of the page.
To display custom cell content, you can create a custom cell renderer class in ActionScript that extends the CellRenderer class or implements the ICellRenderer interface.
The following example creates a custom cell renderer, called MyRenderer, by extending the ImageCell class, which is the default cell renderer class for the TileList. The MyRenderer class creates a static background for the TileList by setting each of the state skins (up, down, over, etc.) to the same skin. It also eliminates the scroll bar and the text overlay, which normally appears behind the label, by setting the scrollPolicy property to off and setting the ImageCell's textOverlayAlpha style to 0.0 to make the overlay transparent. The result of these settings produces a static palette for the images. The example also raises the label from the bottom edge of the image by setting the textPadding style to 15.
There are two parts to this example:
The following steps create the application.
import fl.controls.TileList;
// create TileList instance and position it
var myTileList:TileList = new TileList();
myTileList.move(10,10);
// create TextFormat object to style labels
var tf:TextFormat = new TextFormat();
tf.font = "Verdana";
tf.bold = true;
tf.color = 0xFF6666;
tf.size = 14;
// add content to the TileList
myTileList.addItem({label:"Montreal", source:"http://www.helpexamples.com/images/montreal/images/IMG_5057.jpg"});
myTileList.addItem({label:"Swan", source:"http://www.helpexamples.com/flash/images/gallery1/images/pic11.jpg"});
myTileList.addItem({label:"Sunset", source:"http://www.helpexamples.com/flash/images/gallery1/images/pic14.jpg"});
myTileList.addItem({label:"Gate", source:"http://www.helpexamples.com/flash/images/gallery2/images/IMG_1592.jpg"});
// set column and row values
myTileList.columnWidth = 200;
myTileList.rowHeight = 140;
myTileList.columnCount = 2;
myTileList.rowCount = 2;
myTileList.height = 290;
myTileList.width = 410;
// disable scrolling
myTileList.scrollPolicy = "off";
// add to the display (Stage)
addChild(myTileList);
// set style for labels
myTileList.setRendererStyle("textFormat", tf);
myTileList.setRendererStyle("imagePadding", 5);
// set the background skin
myTileList.setStyle("skin", darkBackground);
//set the cell renderer
myTileList.setStyle("cellRenderer", MyRenderer);
myTileList.setStyle("contentPadding", 5);
The next set of steps create the custom cell renderer class, MyRenderer.
package
{
import fl.controls.listClasses.ICellRenderer;
import fl.controls.listClasses.ImageCell;
public class MyRenderer extends ImageCell implements ICellRenderer
{
public function MyRenderer()
{
// set state skins to same skin so background is static
setStyle("upSkin", darkBackground);
setStyle("downSkin", lightBackground);
setStyle("overSkin", lightBackground);
setStyle("selectedUpSkin", mediumBackground);
setStyle("selectedDownSkin", lightBackground);
setStyle("selectedOverSkin", lightBackground);
// turn off text background by setting to 0
setStyle("textOverlayAlpha", 0.0);
setStyle("textPadding", 15);
}
}
}
To run the application:
To get the source files for this example, download TileList_render.zip from the top of the page.
For more information about this topic, see "Use the TileList component" in Using ActionScript 3.0 Components and the TileList class in the ActionScript 3.0 Component and Language Reference.