简体   繁体   English

Flex/AS3 拖放 - 自定义拖放反馈

[英]Flex/AS3 Drag Drop - Custom Drop Feedback

I am using a HorizontalList component to display a list of images, you can drag images from another component to join the list, this all works fine.我正在使用HorizontalList组件来显示图像列表,您可以从另一个组件中拖动图像加入列表,这一切都很好。

If I do list.showDropFeedback(event) I get an unsightly black bar at the top of images in the HorizontalList - what I really want is a line to the left/right of the image, where the new one will actually sit.如果我这样做list.showDropFeedback(event)我会在HorizontalList ntalList 的图像顶部看到一个难看的黑条 - 我真正想要的是图像左/右的一条线,新的将实际坐在那里。

I guess I need to define a custom DropFeedback to override the default.我想我需要定义一个自定义 DropFeedback 来覆盖默认值。 Does anyone know if there there is a way to achieve this?有谁知道是否有办法实现这一目标?

Thanks!谢谢!

Yuo can sove it by overriding showDropFeedback() method. Yuo 可以通过重写 showDropFeedback() 方法来解决它。 My code below:我的代码如下:

    import mx.controls.HorizontalList;
import mx.controls.listClasses.IListItemRenderer;
import mx.core.mx_internal;
import mx.events.DragEvent;

use namespace mx_internal;

public class HList extends HorizontalList
{
    public function HList()
    {
        super();                        
    }   


        override public function showDropFeedback(event:DragEvent):void
        {

           super.showDropFeedback(event);

            dropIndicator.setActualSize(rowHeight - 4, 4);
            DisplayObject(dropIndicator).rotation = 90;

        }



}

I solved this eventually by adding the following in my Application style..我最终通过在我的应用程序样式中添加以下内容来解决这个问题..

HorizontalList {
 dropIndicatorSkin: ClassReference("com.package.HorizontalListDropIndicator");
} 

And creating my custom skin..并创建我的自定义皮肤..

package com.package {

 import flash.display.Graphics;
 import mx.skins.ProgrammaticSkin;

 public class HorizontalListDropIndicator extends ProgrammaticSkin {

  public function HorizontalListDropIndicator() {
   super();
  }

  override protected function updateDisplayList(w:Number, h:Number):void {  
   super.updateDisplayList(w, h);

   var g:Graphics = graphics;

   g.clear();
   g.lineStyle(2, 0xFF0000);

   g.moveTo(0, 0);
   g.lineTo(0, 250);

  }
 }
}

It looks like this is a bug in the Flex Framework:看起来这是 Flex 框架中的一个错误:

Flex Builder 3/sdks/3.3.0/frameworks/projects/framework/src/mx/controls/HorizontalList.as (lines 95-105) Flex Builder 3/sdks/3.3.0/frameworks/projects/framework/src/mx/controls/HorizontalList.as HorizontalList.as(第 95-105 行)

public function HorizontalList()
{
    super();

    _horizontalScrollPolicy = ScrollPolicy.AUTO;
    _verticalScrollPolicy = ScrollPolicy.OFF;

    direction = TileBaseDirection.VERTICAL;
    maxRows = 1;
    defaultRowCount = 1;
}

Notice that the constructor for HorizontalList is initializing the direction value to Vertical .请注意, HorizontalList的构造函数将方向值初始化为Vertical

A quick and easy work-around is to simply specify direction="horizontal" in your MXML:一个快速简单的解决方法是在 MXML 中简单地指定direction="horizontal"

<mx:HorizontalList id="fooLst" direction="horizontal" dataProvider="{foo}" />

I would be surprised if this bug hasn't already been fixed (and waiting for the next release), but I will check the Flex SDK bug database and submit a report if it's not documented.如果此错误尚未修复(并等待下一个版本),我会感到惊讶,但我会检查Flex SDK 错误数据库,如果没有记录,请提交报告。

There is a style property called dropIndicatorSkin that controls the look of the line that is shown when you drag over a list based component.有一个名为 dropIndicatorSkin 的样式属性,它控制当您在基于列表的组件上拖动时显示的线条的外观。 From the adobe docs:来自 adobe 文档:

dropIndicatorSkin下降指示器皮肤

The skin to use to indicate where a dragged item can be dropped.用于指示可以将拖动项目放置在何处的外观。 When a ListBase-derived component is a potential drop target in a drag-and-drop operation, a call to the showDropFeedback() method makes an instance of this class and positions it one pixel above the itemRenderer for the item where, if the drop occurs, is the item after the dropped item.当 ListBase 派生的组件是拖放操作中的潜在放置目标时,调用showDropFeedback()方法会生成此 class 的实例,并将其定位在itemRenderer上方一个像素的位置,如果放置发生,是被丢弃的物品之后的物品。 The default value is mx.controls.listClasses.ListDropIndicator .默认值为mx.controls.listClasses.ListDropIndicator

All of the functionality is built into the HorizontalList to do what you're asking, but there seems to be a bug in the HorizontalList in which it does not set its direction property to horizontal.所有功能都内置在 HorizontalList 中以执行您的要求,但 HorizontalList 中似乎存在一个错误,它没有将其方向属性设置为水平。

All you need to do is put direction="horizontal" in the mxml declaration and it will rotate the dropIndicatorSkin 90 degrees and poistion it in between the items instead of above them:您需要做的就是将 direction="horizontal" 放在 mxml 声明中,它会将 dropIndicatorSkin 旋转 90 度并将其放置在项目之间而不是它们之上:

<mx:HorizontalList ... direction="horizontal" ... />

Thanks, this seems to rotate the dropIndicator as expected but produces other very unexpected behaviour.谢谢,这似乎按预期旋转了 dropIndicator,但会产生其他非常意外的行为。 The horizontal scroll bar on the list suddenly disappears and dragging an item onto the list makes it skip straight to the end.... Feel's like I'm almost there, but not quite!列表上的水平滚动条突然消失,将一个项目拖到列表上使其直接跳到最后......感觉就像我快到了,但不完全!

My AS3 code....我的 AS3 代码....

// in constructor... (extends HorizontalList)
this.direction = "horizontal";

this.addEventListener(DragEvent.DRAG_ENTER, onDragEnter);
this.addEventListener(DragEvent.DRAG_OVER, onDragOver);
this.addEventListener(DragEvent.DRAG_EXIT, onDragExit);
this.addEventListener(DragEvent.DRAG_DROP, onDragDrop);

private function onDragEnter(event:DragEvent):void {
    event.preventDefault();
    if (event.dragSource.hasFormat("treeItems") || event.dragSource.hasFormat("items")) {
        DragManager.acceptDragDrop(event.target as UIComponent);
    }
    this.showDropFeedback(event);
}

public function onDragOver(event:DragEvent):void {
    event.preventDefault();
    this.showDropFeedback(event);
}

public function onDragExit(event:DragEvent):void {
    event.preventDefault();
    this.showDropFeedback(event);
}

private function onDragDrop(event:DragEvent):void {
    event.preventDefault();
    this.hideDropFeedback(event);
    //....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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