Flex Gotcha #2859 - Datagrids/List-Based Controls and Recycling

Let's imagine that you, for some reason, have a datagrid with 1000 rows. Lets also say that there's a few itemRenderers in there as well that change based on the data for that row.

Well, you might become a bit frustrated to find out that as you scroll through your massive list of items, it will seem like items are getting recycled. You couldn't be more correct.

As a matter of efficiency, many list-based items such as DataGrids don't render 1000 unique itemRenderers for your 1000-item data source. Instead, they only render enough to be visible, plus a couple as a buffer of sorts. So, as you scroll through the list, each one of the original itemRenderers is recycled.

The best example I've seen of this can be seen here...

http://devel.teratechnologies.net/steve_examples/Flex_DataGridProblem/Da...
(scroll up and down in these dataGrids and watch the magic happen)

So, how do we fix this?
Well, it's actually quite simple. Basically you need to make sure that each time the itemRenderer receives data, you need to intercept that event and reset or set the itemRenderer appropriately. This is done by overriding the set data() method...


public override function set data(value:Object):void{
super.data = value; // Kick off the FlexEvent.DATA_CHANGE Event

if(value == null){
// reset the item renderer
return;
}else{
// do something with the data
}
}

It's imperative that you get it in your head that an itemRenderer exists only to create a unique visualization of data, not to do much else. The sooner you realize this, the better your life will be.

We look forward to posting more Flex gotchas in the future, so check back!

Happy Flexing!
kasey mccurdy

Here's some more articles to get this pounded into your brain...

http://www.teratechnologies.net/stevekamerman/index.php?entry=entry08011...

http://www.adobe.com/devnet/flex/articles/itemrenderers_pt1.html

http://blogs.adobe.com/aharui/2007/03/thinking_about_item_renderers_1.html

http://techmytongue.blogspot.com/2009/04/reuse-itemrenderers-for-multipl...