Flex4 拖放功能实现

 
 

 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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值