getScrollPositionDeltaToElement 方法不考虑嵌套子项。为此,您可以使用 mx_internal 方法,如下所示:
/**
* Focus in handler to be used on form elements inside a Scroller. If the
* widgets are inside a FormItem, this ensures that the entire FormItem is
* scrolled into view. Also, if there are validations triggered on focusOut
* of the elements, the default behavior in Flex 4 is to display the error
* messages at the top of the form. Because this affects the vertical position
* of each element, the logic to scroll the item into view must be delayed
* until the next frame using callLater()
*
* NOTE: This uses a method, in the mx_internal namespace
*/
protected function widgetFocusInHandler(evt:FocusEvent):void {
//we need to delay this because we may need to account
//for validation errors being display above the form.
callLater(function(field:UIComponent) : void {
//find the form item that wraps the input and scroll
//it into view
var formItem:DisplayObjectContainer = field.parent;
while (!(formItem is FormItem) && formItem) {
formItem = formItem.parent;
}
//if this item wasn't in a form item, then just use the
//widget itself
if (!formItem) {
formItem = field;
}
var pt:Point = formItem.localToGlobal(new Point(0, formItem.height));
pt = scrollWrapper.globalToLocal(pt);
var layout:LayoutBase = scrollWrapper.layout;
var delta:Point = layout.mx_internal::getScrollPositionDeltaToAnyElement(field);
if (delta) {
if(delta.y > 0) {
layout.verticalScrollPosition += delta.y + 20;
} else if (delta.y < 0) {
layout.verticalScrollPosition += delta.y - 20;
}
}
}, [UIComponent(evt.currentTarget)]);
}