Option类:
package { import flash.display.Shape; import flash.events.Event; import flash.events.MouseEvent; import flash.filters.GlowFilter; /** * 列表框子控件--列表选项框(单个选项) * @author Jave.Lin */ public class Option extends Control { private var _isDrawBorder:Boolean=false;//是否绘制边框 private var _mask:Shape; private var _isDowned:Boolean=false; private var _glowFilter:GlowFilter; //数据对象 private var _data:Object; //显示数据对象文本 private var _textbox:TextBox; public function get isDrawBorder():Boolean { return _isDrawBorder; } public function set isDrawBorder(value:Boolean):void { if(_isDrawBorder!=value) { _isDrawBorder=value; refreshBackground(); } } public function get textColor():uint { return _textbox.textColor; } public function set textColor(value:uint):void { _textbox.textColor=value; } public override function get width():Number { return super.width; } public override function set width(value:Number):void { if(width!=value) { _w=value; _textbox.width=_w; refreshBackground(); } } public override function get height():Number { return super.height; } public override function set height(value:Number):void { if(height!=value) { _h=value; _textbox.height=_h; refreshBackground(); } } public function get data():Object { return _data; } public function set data($data:Object):void { if(_data!=$data) { _data=$data; if(_data) { _textbox.text=_data.toString(); } else { _textbox.text=''; } refreshBackground(); } } public function Option($data:Object) { super(); data=$data; } public function setWithAndHeight($width:Number,$height:Number):void { _textbox.width=_w=$width; _textbox.height=_w=$height; refreshBackground(); } protected override function initialize():void { this.mouseChildren=false; _textbox=new TextBox(); _textbox.isDrawBorder=false; _textbox.isReadOnly=true; _textbox.isSelectable=false; _textbox.fontSize=10; addChild(_textbox); _mask=new Shape(); addChild(_mask); this.mask=_mask; textColor=0; _glowFilter=new GlowFilter(0x00ff00,1,3,3,3); if(stage) { onAddedToStageHandler(); } else { addEventListener(Event.ADDED_TO_STAGE,onAddedToStageHandler); } } private function onAddedToStageHandler(e:Event=null):void { removeEventListener(Event.ADDED_TO_STAGE,onAddedToStageHandler); addEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStageHandler); addEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler); addEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler); } private function onRemovedFromStageHandler(e:Event):void { removeEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStageHandler); removeEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler); removeEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler); } private function onMouseOutHandler(e:MouseEvent):void { removeEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler); addEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler); filters=null; } private function onMouseOverHandler(e:MouseEvent):void { removeEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler); addEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler); filters=[_glowFilter]; } protected override function refreshBackground():void { this.graphics.clear(); this.graphics.beginFill(0x00ff00,.2); this.graphics.drawRect(0,0,width,height); this.graphics.endFill(); if(_isDrawBorder) { this.graphics.lineStyle(1); this.graphics.moveTo(0,0); this.graphics.lineTo(width-1,0); this.graphics.lineTo(width-1,height-1); this.graphics.lineTo(0,height-1); this.graphics.lineTo(0,0); } _mask.graphics.clear(); _mask.graphics.beginFill(0x00ff00,.2); _mask.graphics.drawRect(0,0,width,height); _mask.graphics.endFill(); } public override function toString():String { return "Option ["+_data+"]"; } } }
package { import controlsEvents.OptionsListEvent; import flash.display.Shape; import flash.display.Sprite; import flash.events.MouseEvent; import flash.utils.Dictionary; /** * 多选项选择框列表类 * @author Jave.Lin */ public class OptionsList extends Control { private var _itemsContainer:Panel;//子项容器 private var _itemsControlDic:Dictionary;//子顶控制列表 private var _isShow:Boolean=true;//是否显示 private var _selectedItem:Option; private var _selectedIndex:int; private var _items:Vector.<Object>;//子项所有数据 public function get items():Vector.<Object> { return _items; } //获取当前选中的子项控件,里面包括数据 public function get selectedItem():Option { return _selectedItem; } //获取当前选中子项的索引 public function get selectedIndex():int { return _selectedIndex; } //设置当前选中子项的索引 public function set selectedIndex(value:int):void { if(_selectedIndex!=value) { if(value>_items.length-1) { throw new Error("set OptionsList.selectedIndex error,value:"+value); } if(value<0) { if(value==-1) { _selectedIndex=value; _selectedItem=null; } else { throw new Error("set OptionsList.selectedIndex error,value:"+value); } } else { _selectedIndex=value; _selectedItem=findOptionByData(_items[_selectedIndex]); } dispatchEvent(new OptionsListEvent(OptionsListEvent.SELECTED_CHANGED)); } } public override function get width():Number { return super.width; } public override function set width(value:Number):void { if(width!=value) { _w=value; _itemsContainer.width=_w; for each (var o:Option in _itemsControlDic) { o.width=_w; } } } public override function get height():Number { return super.height; } public override function set height(value:Number):void { if(height!=value) { _h=value; _itemsContainer.height=_h; } } //是否显示 public function get isShow():Boolean { return _isShow; } public function set isShow(value:Boolean):void { if(_isShow!=value) { _isShow=value; } } public function OptionsList() { super(); } public function addItem(obj:Object):void { if(obj==null) { throw new Error("OptionsList.addItem obj is null"); } _items.push(obj); var o:Option=new Option(obj); o.addEventListener(MouseEvent.CLICK,onOptionClickHandler); _itemsContainer.addChild(o); _itemsControlDic[obj]=o; o.y=(_items.length-1)*o.height; o.width=_w; _itemsContainer.refreshScroller(); } private function onOptionClickHandler(e:MouseEvent):void { var o:Option=e.target as Option; if(o==null)return; _selectedItem=o; selectedIndex=_items.indexOf(o.data); } public function addItemRange(arr:Array):void { for each (var obj:Object in arr) { if(obj==null) { throw new Error("OptionsList.addItem obj is null"); } _items.push(obj); var o:Option=new Option(obj); o.addEventListener(MouseEvent.CLICK,onOptionClickHandler); _itemsContainer.addChild(o); _itemsControlDic[obj]=o; o.y=(_items.length-1)*o.height; o.width=_w; } _itemsContainer.refreshScroller(); } public function removeItem(obj:Object):void { if(obj==null) { throw new Error("OptionsList.removeItem obj is null"); } removeOptionByData(obj); } //根据数据:删除数据及控件 public function removeOptionByData($data:Object):Option { if($data==null) { throw new Error("OptionsList.removeComboBoxByData $data is null"); } //删除数据 var index:int=_items.indexOf($data); if(index!=-1) { _items.splice(index,1); } //删除控件 var option:Option=findOptionByData($data); if(option) { delete _itemsControlDic[$data];//从字典中删除:控件 _itemsContainer.removeChild(option);//从控件容器中删除:控件 } onSortControl(index); return option; } //根据数据找 public function findOptionByData($data:Object):Option { return _itemsControlDic[$data]; } //排列控件 private function onSortControl(startIndex:int=-1):void { var i:int=0; if(startIndex!=-1)i=startIndex; for (; i < _items.length; i++) { var cbo:Option=findOptionByData(_items[i]); cbo.y=i*cbo.height; } } protected override function initialize():void { _items=new Vector.<Object>(); _itemsControlDic=new Dictionary(); _itemsContainer=new Panel(); addChild(_itemsContainer); _itemsContainer.isUseVs=true; } protected override function refreshBackground():void { // nothing to do } } }
测试类:
package test { import controlsEvents.OptionsListEvent; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.utils.setInterval; public class OptionsListTest extends Sprite { private var author:Label; private var comboBoxOptions:OptionsList; public function OptionsListTest() { super(); stage.color=0xccccbb; stage.frameRate=60; stage.align=StageAlign.TOP_LEFT; stage.scaleMode=StageScaleMode.NO_SCALE; author=new Label(); addChild(author); author.textColor=0x00ff00; author.fontSize=24; author.x=100; author.y=50; author.text="作者:Jave.Lin"; comboBoxOptions=new OptionsList(); addChild(comboBoxOptions); comboBoxOptions.width=200; comboBoxOptions.height=150; comboBoxOptions.x=100; comboBoxOptions.y=100; var arr:Array=[]; for (var i:int = 0; i < 20; i++) { arr[i]="test"+i; } comboBoxOptions.addItemRange(arr); comboBoxOptions.addEventListener(OptionsListEvent.SELECTED_CHANGED,onSelectedChangedHandler); // setInterval(function():void{comboBoxOptions.removeItem("test5");},1000); // setInterval(function():void{comboBoxOptions.removeItem("test1");},2000); } private function onSelectedChangedHandler(e:OptionsListEvent):void { var cbos:OptionsList=e.target as OptionsList; if(cbos==null)return; trace("cbos",cbos); trace("cbos.selectedIndex",cbos.selectedIndex); trace("cbos.selectedItem",cbos.selectedItem); } } }
运行图片效果: