XML提供数据源
<?xml version="1.0" encoding="utf-8"?> <menuitems> <children label="Open" icon="openedIcon"/> <children label="New" icon="plusIcon"/> <children label="Save" icon="saveIcon"/> <children label="Colse" icon="closeIcon"/> <children label="Undo" icon="undoIcon"/> <children label="Cut" icon="cutIcon"/> <children label="Copy" icon="copyIcon"/> <children label="Paste" icon="pasteIcon"/> </menuitems>
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="init()"> <fx:Script> <![CDATA[ import mx.collections.ArrayCollection; ]]> </fx:Script> <fx:Declarations> <fx:XML id="dataList" source="dataList.xml" /> <s:XMLListCollection id="menu" source="{dataList.children}"/> </fx:Declarations> <fx:Script> <![CDATA[ import mx.events.DragEvent; import mx.core.UIComponent; import mx.managers.DragManager; import mx.controls.Alert; protected var savedItems:ArrayCollection; protected function init():void { list_new.addEventListener(DragEvent.DRAG_ENTER,onDragEnter); list_new.addEventListener(DragEvent.DRAG_DROP,onDragDrop); } protected function onDragEnter(event:DragEvent):void { // TODO var items:Vector.<Object> = event.dragSource.dataForFormat("itemsByIndex") as Vector.<Object>; // isActive 是自己设定的标志位 if(items[0].isActive) { // 接受拖动 DragManager.acceptDragDrop(event.target as UIComponent); } else { // 阻止使用默认的功能 DragManager.showFeedback(DragManager.NONE); event.preventDefault(); Alert.show("Failed!"); } } protected function onDragDrop(event:DragEvent):void { // 同样可以在放置的时候添加过滤条件 } ]]> </fx:Script> <s:Panel title="Drag" id="drag" verticalCenter="0" horizontalCenter="0" width="450" height="300"> <s:layout> <s:HorizontalLayout paddingLeft="20" paddingTop="20"/> </s:layout> <s:HGroup> <!--dragEnable控制是否可被拖拽--> <!--dragMoveEnable默认是false,即拖拽对于数组影响是复制,设置为true的时候 使其动作为移动--> <!--同一个空间内在再添加上dropEnable就是可以在本空间内拖拽重新排序--> <!--allowMultipleSelection允许多行操作的D&D属性--> <s:List id="list_old" dragEnabled="true" dropEnabled="true" dragMoveEnabled="true" dataProvider="{menu}" labelField="@label" allowMultipleSelection="true"/> <!--dropEnable控制是否可以放置--> <s:List id="list_new" dragEnabled="true" dropEnabled="true" dragMoveEnabled="true" dataProvider="{new ArrayCollection()}" labelField="@label" allowMultipleSelection="true"/> </s:HGroup> </s:Panel> </s:Application>