开发者

Adobe Flex Prevent Double Click

I have an adobe flex datagrid with a simple custom label itemrenderer in one of the columns. Clicking on the itemrenderer fires off a cairngorm event, which results in a service call being made, and the data from the service call populates a TitleWindow.

Even though I have set the doubleClickedEnabled property to false, its still possible to sometimes double click the item renderer, resulting in two service calls being made, and two TitleWindows being show开发者_如何学运维n. In other cases, triple clicking seems to trigger the undesired result as well.

Is there any method to prevent this kind of behaviour?


Keep track of when you get a click event and if you get two within a certain amount of time (250ms) ignore the second.

var lastTime:Date = new Date(0);

function OnClickHandler(event:Event):void {
    if ((new Date()).time - lastTime.time < 250) {
        return;
    }
    lastTime = new Date();
    ...
}


Ok, I know this is old but the easiest way to prevent the double click event is to set doubleClickEnabled = "true" for the button and to not assign any function to the doubleClick event. Thus the button recognizes the second click but does not dispatch a single click event. Hope it makes sense - I have tested this and works for me :)


// class variable section
var lastTime:Date = null;

...

function handler(evt:Event):void{
    var currentTime = new Date();
    if (lastTime === null){
         lastTime = currentTime;
    } else {
         var diff = currentTime.milliseconds() - lastTime.milliseconds();
         if (diff <= 250) // try some other intervals to get best result for you.
             return;
         else 
             lastTime = currentTime;
    }
    ... the rest of your code.
}


The best way to do this is to set a flag in your itemRenderer to note that it has been clicked once. This prevents it from sending another cairngorm event until the result of the first one is returned, at which time you reset the flag, enabling it to be clicked again.

For brevity, the following code assumes everything is happening within the same scope. If it isn't you'll have to send and receive events to and from the itemRender from some other context.

[Bindable]
var _clickEnabled:Boolean = true;

private function doClickStuff() : void {
  if (_clickEnabled) {
    // fire your event
    _clickEnabled = false;
  } 
}

private function onEventReturnHandler(event:Event) : void {
  // do whatever it is you're going to do
  _clickEnabled = true;
}


A simpler solution that will work to prevent double-clicks from sending two service calls is to enable double click and have double click event call the same handler as the single click event.

<s:Button label="Button"
          click="clickHandler()"
          doubleClick="clickHandler()"
          doubleClickEnabled="true" />

Of course, this does not prevent triple clicks, but this is a far less frequent problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜