Search code examples
actionscript-3flex4itemrenderer

how to refresh item renderers and change their state?


I have a list with time related data. the item renderers display this time. I would like to make the item renderers refresh their state/labels upon the tick of a timer.

to which event should I add a listener within the renderer, and how do I trigger such event from the list?


Solution

  • Hmm.. Usually you don't want to manipulate the item renderer directly. Typically you would update the collection.

    If the timer reference is originally set from the data provider, you might want to set a timer and manipulate that property in the collection...

    Something like this:

    //Timer listener function (dp is data provider)
    protected function handleTimerEvent(event:timerEvent):void
    {
        for(var i:int = 0; i < dp.length; i++)
        {
            var o:Object = dp.getItemAt(i);
            o.minutes += 1;
    
            if(o.minutes == 60)
            {
                o.minutes = 0;
                o.hour += 1
            }
        }
    
        dp.refresh();
    }
    

    If minutes and hour is referenced in your item renderer you should see it update for each one. Of course, you would have to add better logic to handle other date stuff. You may want to make use of the date class to show more accurate time information.

    Using the timer event in the item renderer may give you a performance hit depending on the length of your dataprovider. Say you have 100 rows of data, that's 100 timer events!