Avg. Rating 4.0

Problem

If you really need to change a list item renderer dynamically.

Solution

It is very simple: you can do this by using ClassFactory.

Detailed explanation

you can simply do this by using  Class Factory.ie  you need to create two item render for the list
when you want to change one item render by another just pas the class name  of the second item render to your list
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*" creationComplete="init()">
    <mx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
           [Bindable]
           private var  myData:ArrayCollection;
           private function init():void
           {
               myData=new  ArrayCollection([{name:'nandu'},{name:'gopu'},{name:'john'}]);
               
           }
            private function changeItemRender():void
            {
                myList.itemRenderer=new ClassFactory(Render2);//just pass the class name not its reference
            }
        ]]>
    </mx:Script>
    
    <mx:List id="myList" dataProvider="{myData}">
    <mx:itemRenderer>
        <mx:Component>
            <local:Render1/>
        </mx:Component>
    </mx:itemRenderer>
    </mx:List>
    <mx:Button label="ChageMyRender" click="changeItemRender()" y="192"/>
    
</mx:Application>

Render1.mxml
------------------
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml"  >
    <mx:Label text="i am  render1"/>
    <mx:Label text="{data.name}"/>
  
</mx:HBox>

Render2.mxml
---------------
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml"  creationComplete="init()">
    <mx:Label text="i am  render2"/>
    <mx:Label text="{data.name}"/>
   
</mx:HBox>
 

Report abuse

Related recipes