How to tell when control becomes visible for the first time?
I have a popup that contains a TabNavigator control. The tabs are dynamically added to the TabNavigator when the popup loads. Is there a good way to tell when one of the tabs is loaded, from the tab itself?
I have a tab that requires a s开发者_Python百科ervice call to be made, and I don't want the service call to be made unless the user actually goes and clicks the tab to view it. I could notify the tab from the popup control itself when the TabNavigator index was changed, but that doesn't seem like a good way to go about doing it. I'm wondering if there's an event or something I could hook to that would let me know the tab needed to be rendered for the first time (from within the tab control itself). Thanks in advance!
I think I didn't quite understand the question, so I'm trying again. I think this code will address your problem.
The trick is setting the creation policy to "none" for the component. Flex will create the tab, but not the component itself.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="onCreationComplete()" layout="vertical" minWidth="955" minHeight="600">
<mx:TextArea id="log" width="100%" height="500"/>
<mx:Script>
<![CDATA[
import mx.containers.Canvas;
import mx.events.FlexEvent;
private function onCreationComplete():void{
var canv:Canvas = new Canvas();
canv.label = "One";
canv.addEventListener(FlexEvent.CREATION_COMPLETE,onCreateTab);
canv.creationPolicy="none";
tn.addChild(canv);
canv = new Canvas();
canv.label = "Two";
canv.addEventListener(FlexEvent.CREATION_COMPLETE,onCreateTab);
canv.creationPolicy="none";
tn.addChild(canv);
canv = new Canvas();
canv.label = "Three";
canv.addEventListener(FlexEvent.CREATION_COMPLETE,onCreateTab);
canv.creationPolicy="none";
tn.addChild(canv);
}
private function onCreateTab(event:Event):void{
log.text+=event.currentTarget.label+ " created for the very first time\n";
}
]]>
</mx:Script>
<mx:TabNavigator id="tn" width="500"/>
</mx:Application>
There's an event called the "FlexEvent.SHOW" event which should work for you. See: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/events/FlexEvent.html#SHOW
You can put this on the TabNavigator. i.e.
<mx:TabNavigator>
<comp:SomeComp show="doSomething()" label="My Tab"/>
</mx:TabNavigator>
Or you can put it inside your component. i.e.
<mx:Canvas show="doSomething">
<!-- My Component-->
</mx:Canvas>
If you're creation policy is set, you can also do this on creationComplete, but it will only happen the FIRST time the tab is created.
Running example:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" minWidth="955" minHeight="600">
<mx:TextArea id="log" width="100%" height="500">
</mx:TextArea>
<mx:TabNavigator width="500">
<mx:Canvas label="One" show="{log.text+='One Clicked\n';}"/>
<mx:Canvas label="Two" show="{log.text+='Two Clicked\n';}"/>
<mx:Canvas label="Three" show="{log.text+='Three Clicked\n';}"/>
<mx:Canvas label="Four" show="{log.text+='Four Clicked\n';}"/>
</mx:TabNavigator>
</mx:Application>
With dynamic tabs.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="cc()" layout="vertical" minWidth="955" minHeight="600">
<mx:TextArea id="log" width="100%" height="500">
</mx:TextArea>
<mx:Script>
<![CDATA[
import mx.containers.Canvas;
import mx.events.FlexEvent;
private function cc():void{
var canv:Canvas = new Canvas();
canv.label = "One";
canv.addEventListener(FlexEvent.SHOW,onShow);
tn.addChild(canv);
canv = new Canvas();
canv.label = "Two";
canv.addEventListener(FlexEvent.SHOW,onShow);
tn.addChild(canv);
}
private function onShow(event:Event):void{
log.text+=event.currentTarget.label+ " clicked\n";
}
]]>
</mx:Script>
<mx:TabNavigator id="tn" width="500">
</mx:TabNavigator>
</mx:Application>
Read up on the Flex Component LifeCycle. Based on what you describe, I would probably listen for the creationComplete event on the tab's content.
精彩评论