Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache flex Flex-防止拖动某些项目_Apache Flex_Drag And Drop - Fatal编程技术网

Apache flex Flex-防止拖动某些项目

Apache flex Flex-防止拖动某些项目,apache-flex,drag-and-drop,Apache Flex,Drag And Drop,如何防止拖动列表或数据网格中的某些项目 假设我有一个清单,上面有两个项目:“汤姆”和“杰瑞”。只有“汤姆”应该是可拖动的,而不是“杰瑞” 理想情况下,我有一个“isDragEnabled(item:Object):Boolean”函数,该函数由拖动源查询 我的困难首先是“dragStart”事件处理程序的dragSource值为空,因此从一开始我就很难找出dragStart是关于什么的 提前谢谢 PS有一些关于防止或取消拖放的讨论,但我没有看到太多关于防止拖动启动的内容,因此这个问题。您可以做两

如何防止拖动列表或数据网格中的某些项目

假设我有一个清单,上面有两个项目:“汤姆”和“杰瑞”。只有“汤姆”应该是可拖动的,而不是“杰瑞”

理想情况下,我有一个“isDragEnabled(item:Object):Boolean”函数,该函数由拖动源查询

我的困难首先是“dragStart”事件处理程序的dragSource值为空,因此从一开始我就很难找出dragStart是关于什么的

提前谢谢


PS有一些关于防止或取消拖放的讨论,但我没有看到太多关于防止拖动启动的内容,因此这个问题。

您可以做两件事:

  • 根据selectedItem数据对象的属性,可以禁用列表中不可拖动的项。这将导致它们在视觉上被禁用,这可能是您不想要的,因此您也可以尝试

  • 当选择了有效项(基于selectedItem的数据对象)时,可以将列表的dragEnabled属性设置为“true”,而当这些项无效时,可以将其设置为“false”


  • 好的,明白了,谢谢Robusto,你的提示2是灵感来源,但是我不得不使用鼠标按下的侦听器——选择事件启动得太晚了

    在下面的示例中,我使用的代码来自我的

    此示例允许您仅拖动列表或数据网格中的第一项:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal" minWidth="955" minHeight="600">
    
        <mx:List id="list" dataProvider="{['Tom','Jerry', 'Amy', 'Betty', 'Chris', 'Dean', 'Email', 'Floyd', 'Grant', 'Helen', 'Iris', 'Jack']}" minWidth="200"
            mouseDown="onMouseDown(event)"
            />
    
        <mx:DataGrid id="dg" dataProvider="{[{title:'Tom'},{title:'Jerry'}]}" minWidth="200"
             mouseDown="onMouseDown(event)" 
        >
            <mx:columns>
                <mx:DataGridColumn dataField="title" />
            </mx:columns>
        </mx:DataGrid>
    
        <mx:Script>
            <![CDATA[
                import mx.controls.listClasses.ListBase;
                import mx.events.DragEvent;
    
    
                protected function onMouseDown(event:MouseEvent):void
                {
                    var listBaseComp:ListBase = ListBase(event.currentTarget);
                    var clickIndex:int = this.findClickedItemIndex(event.stageX, event.stageY, listBaseComp);
                    listBaseComp.dragEnabled = clickIndex == 0;
                }
    
                /**
                 * Returns a dataProvider item that displays at the given coords for the given dataGrid.
                 * Code provided by Stackoverflow user https://stackoverflow.com/users/165297/amarghosh,
                 * thanks a lot!
                 */
                protected function findClickedItemIndex(globalX:Number, globalY:Number, listComp:ListBase):int
                {
                    var p1 : Point;
                    var p2 : Point;
                    var renderer : DisplayObject;
    
                    for(var i:int=0; i<listComp.dataProvider.length; i++) {
                        renderer = DisplayObject(listComp.indexToItemRenderer(i));
                        if (!renderer) //item is not displayed (scroll to view it)
                            continue;
                        p1 = new Point(renderer.x, renderer.y);
                        p2 = new Point(renderer.width, renderer.height);
                        p1 = renderer.parent.localToGlobal(p1);
                        p2 = renderer.localToGlobal(p2);
                        if(globalX >= p1.x && globalX <= p2.x && globalY >= p1.y && globalY <= p2.y)
                            return i;
                    }   
                    return -1;
                }
    
            ]]>
        </mx:Script>
    </mx:Application>
    
    
    =p1.x&&globalX=p1.y&&globalY
    
    如果要避免拖动项目,应使用以下方法:

    <fx:Script>
        <![CDATA[
           private function onDragStart(event:DragEvent):void {
           var selectedNode:Object = itemsList.selectedItem;
           if (selectedNode is not a draggable item) {
               event.stopImmediatePropagation();
           }
        }
        ]]>
    </fx:Script>
    
    <s:List id="itemsList" dragStart="onDragStart(event)"/>
    
    
    

    DragStart事件在拖动开始时立即调度,因此,如果停止事件的传播,则可以避免拖动项目。

    Good options Robusto,您是否使用/测试了其中任何一个?1看起来很简单,但在视觉上很烦人。2.这似乎行得通,但很多似乎对我有用的东西却不行。我两个都用过,它们对我的公司都有用。请注意,#1使用列表的disabledFunction属性,而#2从dragDrop事件调用处理程序,该事件检查行是否可以拖动,如果行无效,则调用event.stopImmediatePropagation()和event.preventDefault()。