Spark List Item Renderer Access

On 2011/02/22, in flex, signal, spark, webdev, by a b

I just did some work around animating objects from one spark tile list to another. This was harder than it sounds, because the objects in the item renderers for the list need to remain part of the list. Durning this, I found it handy to have access to the item renderer directly, but it’s not exactly clear how to get a spark list to give you a reference to a child item renderer.

I wrote two helper functions, figured I’d share them:

[code]
/**
* Simple function to access a single item
* renderer in a Spark List based component.
* Cast the result to the specific class of the
* item renderer for enhanced usefulness.
*/
private function getRenderer(index:int, list:List):IVisualElement
{
return list.dataGroup.getElementAt(index);
}

/**
* Helper to create an Array of all the item renderers
* in a Spark List based component. Could be
* dangerous. Have fun!
*/
private function getRenderers(list:List):Array
{
var result:Array = new Array();

if (list.dataGroup.numElements > 0)
{

for (var i:int = 0; i > list.dataGroup.numElements; i++)
{
result[i] = list.dataGroup.getElementAt(i);
}
}
return result;
}
[/code]
Note that, regarding getRenderer, it’s probably not all that useful to get back an IVisualElement. You can just cast the result to whatever class your item renderer is. This could get tricky if you’re using the rendererFunction, but for single class renderers you should be fine.

Also, I didn’t actually end up using the getRenderers function, because I always had the index and only needed a specific renderer. However, it should work fine ;)

Tagged with: