简体   繁体   中英

How to implement drag and drop in Flex Grid control?

I have a simple Grid control with some buttons that I want to be able to move around. The code below does work, but it takes a lot of effort to actually do the drag&drop and it is not clear where the drop will happen. I have to move the mouse around a lot to get to a state where the drop is not rejected. I would appreciate any suggestions on how to make this more "user friendly".


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" horizontalAlign="center" height="200" width="200">
    <mx:Script>
        <![CDATA[
import mx.containers.GridItem;
import mx.controls.Button;
import mx.core.DragSource;
import mx.events.*;
import mx.managers.DragManager;

private function dragInit(event:MouseEvent):void
{
    if(event.buttonDown)
    {
        var button:Button = event.currentTarget as Button;
        var dragSource:DragSource = new DragSource();
        dragSource.addData(button, 'button');
        DragManager.doDrag(button, dragSource, event);
    }
}

private function dragEnter(event:DragEvent): void
{
    var target:GridItem = event.currentTarget as GridItem;
    if (event.dragSource.hasFormat('button') && target.getChildren().length == 0)
    {
        DragManager.acceptDragDrop(target);
        DragManager.showFeedback(DragManager.MOVE);
    }
}

private function dragDrop(event:DragEvent): void
{
    var target:GridItem = event.currentTarget as GridItem;
    var button:Button = event.dragSource.dataForFormat('button') as Button;
    target.addChild(button);
}           
        ]]>
    </mx:Script>

    <mx:Grid>
        <mx:GridRow width="100%" height="100%">
            <mx:GridItem width="44" height="44" dragEnter="dragEnter(event)" dragDrop="dragDrop(event)">
            </mx:GridItem>
            <mx:GridItem width="44" height="44" dragEnter="dragEnter(event)" dragDrop="dragDrop(event)">
            </mx:GridItem>
            <mx:GridItem width="44" height="44" dragEnter="dragEnter(event)" dragDrop="dragDrop(event)">
                <mx:Button label="A" width="40" height="40" mouseMove="dragInit(event)"/>
            </mx:GridItem>
        </mx:GridRow>
        <mx:GridRow width="100%" height="100%">
            <mx:GridItem width="44" height="44" dragEnter="dragEnter(event)" dragDrop="dragDrop(event)">
            </mx:GridItem>
            <mx:GridItem width="44" height="44" dragEnter="dragEnter(event)" dragDrop="dragDrop(event)">
                <mx:Button label="B" width="40" height="40" mouseMove="dragInit(event)"/>
            </mx:GridItem>
            <mx:GridItem width="44" height="44" dragEnter="dragEnter(event)" dragDrop="dragDrop(event)">
            </mx:GridItem>
        </mx:GridRow>
        <mx:GridRow width="100%" height="100%">
            <mx:GridItem width="44" height="44" dragEnter="dragEnter(event)" dragDrop="dragDrop(event)">
                <mx:Button label="C" width="40" height="40" mouseMove="dragInit(event)"/>
            </mx:GridItem>
            <mx:GridItem width="44" height="44" dragEnter="dragEnter(event)" dragDrop="dragDrop(event)">
            </mx:GridItem>
            <mx:GridItem width="44" height="44" dragEnter="dragEnter(event)" dragDrop="dragDrop(event)">
            </mx:GridItem>
        </mx:GridRow>
    </mx:Grid>
    <mx:Style>
GridItem
{
    borderColor: #A09999;
    borderStyle: solid;
    borderThickness: 2;
    horizontal-align: center;
    vertical-align: center;
}
Grid
{
    borderColor: #A09999;
    borderStyle: solid;
    borderThickness: 2;
    horizontalGap: 0;
    verticalGap: 0;
    horizontal-align: center;
    vertical-align: center;
}
    </mx:Style>
</mx:Application>


It looks like each empty grid item only exists on the border you've defined (I'm not sure why this is, possibly it's a known feature of containers). If you add a backgroundColor to each GridItem then dragEnter will be triggered over the whole area rather than just that border.

只需在您的GridItem放入backgroundColor :)

不幸的是,您必须处理所有这些事件。

Define a background color to the Grid component. After that, you can drop the buttons in the middle of a GridItem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM