Flex 3: Override editable list's keyDownHandler gives focus issue
I'm trying to override the default handling of the 'up' and 'down' keys for an editable list, such that when they are pressed a list entry that is being edited behaves as if the escape key has been pressed, but the 'selected item bar' moves up or down.
Here is my (simplified) component:
<?xml version="1.0" encoding="utf-8"?>
<mx:List xmlns:mx="http://www.adobe.com/2006/mxml"
enabled="true" width="100%" height="100%"
creationComplete="initialise()"
editable="true"
>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
private function initialise():void
{
dataProvider = new ArrayCollection(["this", "is", "a", "very", "simple", "example"]);
}
override protected function keyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.ESCAPE
|| event.keyCode == Keyboard.DOWN
|| event.keyCode == Keyboard.UP)
endEdit("ENDEDIT"); // Same behaviour with destroyItemEditor();
super.keyDownHandler(event);
//callLater(this.setFocus);
}
]]>
</mx:Script>
For some reason this doesn't work: if a row is being edited, the first 'up' (or down) keypress ends the edit and moves the bar. Further up or down keypresses have no effect, but if you click somewhere near the list it does start working. This makes me think it is a focus开发者_JAVA技巧 issue. Weirdly, if you hit escape the edit finishes and you can move up and down correctly.
If the 'callLater' line is uncommented to try and make the list retain focus, the bar initially moves in the direction of the keypress but then moves back again and re-edits the original line !
Anyone got any ideas ?
Thanks in advance,
Mike
I've been attempting to follow the source, and the only 'reason' provided for an endEdit() call is always ListEventReason.CANCELLED ("cancelled"), which may be your problem.
The reason this would still work with the escape key is that that is handled by the editorKeyDownHandler() function of the List, which is private (nah-nah, you can't override me).
You are going to have to extends List to override the inherited keydown behavior. However, I got it to work for the first UP or DOWN after clicking on a row but I can't seem to continue the subsequent keydown events:
<?xml version="1.0" encoding="utf-8"?>
<mx:List xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="{creationCompleteHandler()}">
<mx:Script>
<![CDATA[
override protected function keyDownHandler(event:KeyboardEvent):void
{
if (itemEditorInstance)
{
destroyItemEditor();
if (event.keyCode == Keyboard.UP)
{
if (selectedIndex == 0)
{
selectedIndex = this.dataProvider.length - 1;
}
else
{
selectedIndex--;
}
}
else if (event.keyCode == Keyboard.DOWN)
{
if (selectedIndex == this.dataProvider.length - 1)
{
selectedIndex = 0;
}
else
{
selectedIndex++;
}
}
}
super.keyDownHandler(event);
}
]]>
</mx:Script>
精彩评论