3

我想显示一个列表(在磁贴布局中,但我认为这不会改变任何东西)并打开我长按的磁贴的编辑窗口(即按下至少 1 秒)。计时器启动正常,并且 longPressHandler 事件在 1 秒后正确调用,但是,选择的对象是我触摸的上一个对象。

例如,假设我点击对象 A,然后长按对象 B:longPressHandler 将在编辑窗口中“打开”对象 A。我已经调试过,我看到我的 List 的 SelectedItem 属性仅在我结束长按后更新(例如,在我拿起手指或松开鼠标按钮后)。有没有办法打开当前选中的项目?


相关动作脚本:

private  var longPressTimer:Timer = new Timer(1000,1);

private function startLongPressMouse(event:MouseEvent):void {   
    startLongPressTimer();
    list.addEventListener(MouseEvent.MOUSE_MOVE, endLongPressMouse);
}
private function endLongPressMouse(event:MouseEvent):void {
    stopLongPressTimer();
    enableClick();
    list.removeEventListener(MouseEvent.MOUSE_MOVE, endLongPressMouse);
}
private function startLongPress(event:TouchEvent):void {
    startLongPressTimer();
    list.addEventListener(TouchEvent.TOUCH_MOVE, endLongPress);
}
private function endLongPress(event:TouchEvent):void {
    stopLongPressTimer();
    enableClick();
    list.removeEventListener(TouchEvent.TOUCH_MOVE, endLongPress);
}
private function startLongPressTimer():void {
    longPressTimer.start();
    longPressTimer.addEventListener(TimerEvent.TIMER_COMPLETE, longPressHandler);
}
protected function disableClick():void {
    trace("disable click");
    list.removeEventListener(MouseEvent.CLICK, regularClickHander);
}
public function enableClick():void {
    list.callLater(list.addEventListener, [MouseEvent.CLICK, regularClickHander]);
}
public function resetListSelection():void {
    list.selectedIndex = -1;
    list.validateDisplayList();
}
private function stopLongPressTimer():void{
    longPressTimer.stop();
    longPressTimer.reset()
}
public function longPressHandler(event:TimerEvent):void{
    disableClick();
    stopLongPressTimer();
    lblD.text = "Long Press Detected on: " + list.selectedItem.className;
}

相关MXML:

 <s:List id="list"  dataProvider="{grades}" touchBegin="startLongPress(event)" touchEnd="endLongPress(event)"
        mouseDown="startLongPressMouse(event)" mouseUp="endLongPressMouse(event)"
        labelField="name"
        left.landscape="10" right.landscape="20" top.landscape="350" bottom.landscape="20"
        left.portrait="20" right.portrait="20" top.portrait="350" bottom.portrait="20">
    <s:itemRenderer>
        <fx:Component>
            <s:ItemRenderer width="100%" height="200">

                <s:Label text="{data.className}" top="30" horizontalCenter="0" color="#646464"/>
                <s:Label text="{data.credits}" top="50" horizontalCenter="0" color="#646464" fontSize="14"/>
                <s:Label text="{data.grade}" top="100" horizontalCenter="0" color="#646464" fontSize="14"/>
            </s:ItemRenderer>
        </fx:Component>
    </s:itemRenderer>
    <s:layout>
        <s:TileLayout requestedColumnCount="3" requestedColumnCount.landscape="4" columnAlign="justifyUsingWidth"/>
    </s:layout>
</s:List>

我将 longPressHandler 更改为仅显示所选项目的名称,而不是打开编辑窗口。

如果您需要更多信息,请告诉我。预先感谢您的帮助!

4

1 回答 1

0

一种方法是创建一个自定义事件(带有自定义属性),这样您就可以在分派事件时传递您想要的任何数据。

package events;
{
    import flash.events.Event;

    public class YourCustomEvent extends Event
    {
        public var yourData:Object;

        public function YourCustomEvent(type:String, yourData:Object)
        {
            super(type);
            this.yourData = yourData;
        }

        override public function clone():Event{
            return new YourCustomEvent(type, yourData);
        }
    }
}

希望能帮助到你!

PS:我想把它放在评论中,但打字时它变得更大了:)

于 2013-02-08T00:41:14.473 回答