代码:自定义的ResizeTree.as

   
   
   
   
  1. package  contr  
  2. {   
  3.     import flash.events.*;  
  4.     import flash.utils.*;  
  5.       
  6.     import mx.controls.Alert;  
  7.     import mx.controls.Tree;  
  8.     import mx.events.TreeEvent;  
  9.       
  10.     public class ResizeTree extends Tree {   
  11.           
  12.         private var timer:Timer = new Timer(10, 1);  
  13.         private var _autoStretch_:Boolean = false;  
  14.         [Bindable]  
  15.         public function get autoStretch():Boolean{   
  16.             return _autoStretch_;   
  17.         }   
  18.           
  19.         public function set autoStretch(value:Boolean):void{  
  20.             _autoStretch_ = value;   
  21.             if (_autoStretch_) {  
  22.                 this.addEventListener(Event.RESIZE, doStretch);  
  23.                 this.addEventListener(TreeEvent.ITEM_OPEN, startStretch);   
  24.                 this.addEventListener(TreeEvent.ITEM_CLOSE, startStretch);   
  25.                 this.verticalScrollPolicy = "off";   
  26.                 stretch();   
  27.             } else {  
  28.                 timer.stop();  
  29.                 this.removeEventListener(Event.RESIZE, doStretch);  
  30.                 this.removeEventListener(TreeEvent.ITEM_OPEN, startStretch);  
  31.                 this.removeEventListener(TreeEvent.ITEM_CLOSE, startStretch);  
  32.                 this.verticalScrollPolicy = "auto";   
  33.             }   
  34.         }   
  35.           
  36.         protected function stretch(evtObj:Event = null):void{  
  37.             var items:Array = this.listItems;   
  38.             var borderThickness:Number = this.getStyle("borderThickness");   
  39.             if (this.height - borderThickness * 2 < this.rowHeight) {  
  40.                 thisthis.height = this.rowHeight + borderThickness * 2;   
  41.             }   
  42.             if (this.maxVerticalScrollPosition > 0) {  
  43.                 this.height += this.rowHeight - (this.height - borderThickness * 2) % this.rowHeight;  
  44.                 return;  
  45.             } else if (items[items.length - 1] == "") {  
  46.                 this.height -this.rowHeight + (this.height - borderThickness * 2) % this.rowHeight;  
  47.                 return;   
  48.             }   
  49.         }   
  50.           
  51.         protected function startStretch(evtObj:Event = null):void{  
  52.             timer.stop();  
  53.             timer.delay = this.getStyle("openDuration") + 50;  
  54.             timer.start();   
  55.         }   
  56.           
  57.         protected function doStretch(evtObj:Event = null):void{  
  58.             timer.stop();   
  59.             timer.delay = 10;  
  60.             timer.start();   
  61.         }  
  62.           
  63.         protected function onTimerComplete(evtObj:Event = null):void{  
  64.             stretch();   
  65.         }   
  66.           
  67.         public function ResizeTree():void{   
  68.             super();   
  69.             timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);   
  70.         }   
  71.     }   
  72. }  

使用:

   
   
   
   
  1. xml version="1.0" encoding="utf-8"?> 
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   
  3.                xmlns:s="library://ns.adobe.com/flex/spark"   
  4.                xmlns:mx="library://ns.adobe.com/flex/mx"   
  5.                minWidth="955" minHeight="600" xmlns:logic="contr.*"> 
  6.     <fx:Declarations> 
  7.          
  8.         <fx:XMLList id="treeData"> 
  9.             <node label="选择数据集"> 
  10.                 <node label="hsql"> 
  11.                     <node label="tabels"> 
  12.                           <node label="订单" head="p_w_picpaths/install.jpg"/> 
  13.                           <node label="订单明细" head="p_w_picpaths/install.jpg"/> 
  14.                           <node label="雇员" head="p_w_picpaths/install.jpg"/> 
  15.                     node>   
  16.                 node> 
  17.                 <node label="mysql"> 
  18.                     <node label="tabels"> 
  19.                           <node label="demo_salesday" head="p_w_picpaths/install.jpg"/> 
  20.                           <node label="Personal" head="p_w_picpaths/install.jpg"/> 
  21.                     node>   
  22.                 node> 
  23.             node> 
  24.         fx:XMLList> 
  25.     fx:Declarations> 
  26.     <mx:VBox backgroundColor="#CCCCCC" width="100%" height="100%" verticalAlign="middle" horizontalAlign="center"> 
  27.         <logic:ResizeTree dataProvider="{treeData}" height="100" autoStretch="true" labelField="@label"  depthColors="[#CCCCCC,#f60f0f,#1c47e8,#31eb18]"/> 
  28.     mx:VBox> 
  29. s:Application> 

效果:点击节点 打开或关闭时 tree的高度会动态变化