问题
I would like to prevent a user dragging certain items from one TileList to another. Whether or not they can be dropped into the TileList should be determined based on data associated with the item.
To show that the item cannot be dragged into the TileList I would like to show the normal white cross in red circle icon next to the cursor. If a drop is attempted when it has been determined that drop should not occur I would like to show an alert message explaining why this item could not be dropped.
回答1:
This article should help you. The example in the article seems exactly what you want.
http://butterfliesandbugs.wordpress.com/2007/10/10/cancelling-a-drag-action-in-list-components/
回答2:
You'll need to prevent the default action on both the dragDrop event (to stop it from being added to the new List) and dragComplete event (to stop it from being removed from the old List). Here is an example for allowing items to be moved within a list but not between lists:
private var preventMove:Boolean = false;
private function onDragDrop(event: DragEvent): void {
preventMove = (event.dragInitiator != event.target);
if (preventMove)
event.preventDefault();
}
private function onDragComplete(event: DragEvent): void {
if (preventMove) {
event.preventDefault();
}
}
You can use whatever criteria you want in place of checking the dragInitiator against the dragDrop target. Along with calling preventDefault on dragComplete you can pop up your error message.
Where you have two Lists (or TileLists or whatever):
<s:List dragDrop="onDragDrop(event)" dragComplete="onDragComplete(event)"
dragEnabled="true" dropEnabled="true" dragMoveEnabled="true">
</s:List>
<s:List dragDrop="onDragDrop(event)" dragComplete="onDragComplete(event)"
dragEnabled="true" dropEnabled="true" dragMoveEnabled="true">
</s:List>
If you don't need to be able to drag and drop things within the same List or you're only dragging one direction it can be simpler just by not enabling everything on both lists.
The article linked by Chetan Sastry is now old and has broken links.
来源:https://stackoverflow.com/questions/544232/how-to-prevent-an-item-being-dropped-into-one-tilelist-from-another-using-flex