Flash Builder Long Press on List - Mobile

I want to display a list (in the tile layout, but I don't think it changes anything) and open an edit window for the tile that I long press on (i.e. press for at least 1 second). The timer starts fine and the longPressHandler event is correctly called after 1 second, however the selected object is the previous object I touched.

For example, say I click on Object A, then I long click on Object B: longPressHandler will "open" object A in the edit window. I have debugged and I see that the SelectedItem property of my list only updates after I finish my long press (like after I lift my finger or release my mouse button, for example). Is there a way to open the selected item?


Relevant ActionScript:

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;
}

      


Corresponding 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>

      


I changed the longPressHandler to just display the name of the selected item and not open an edit window.

Let me know if you need more information. Thank you in advance for your help!

+3


source to share


1 answer


One way is to create a custom event (with a custom property) so you can pass whatever data you want when the event is dispatched.

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);
        }
    }
}

      



Hope this helps!

PS: I wanted to put this in a comment, but it got bigger as printed :)

0


source







All Articles