2

spark Scroller 内に多くの子テキスト入力があります。スクロールバーがその子項目に自動的にスクロールするために、a..z ID がある場合、「x」の ID を持つ TextInput をフォーカスするにはどうすればよいですか?

x.setFocus() を使用できますが、スクロールバーはその項目まで自動的にスクロールしませんか? なぜ?

<s:Scroller id="scroller" width="100%" height="100">
        <s:Group id="group" width="100%" height="100" id="content">
            <s:TextInput id="a" text="" editable="true" width="100%" height="25" />
            <s:TextInput id="b" text="" editable="true" width="100%" height="25" />
            ....
        </s:Group>
</s:Scroller>

ありがとう、フィリップ

4

4 に答える 4

2

その理由は、setFocus はオブジェクトをアクティブにするだけで、実際には ScrollBar の scrollPosition を変更するわけではないためです。List のようなより複雑なクラスを使用すると、より単純になりますが、Scroller は非常に基本的であるため、少し難しくなります。

必要なことを行うには、ビューポート (グループ) 内の要素のインデックスを取得してから、scrollPosition を手動で設定する必要があります。垂直レイアウトの場合、コードは次のようになります。

var index:Number = group.getElementIndex(g);
var offset:Number = group.getElementAt(index).height;
scroller.viewport.verticalScrollPosition = index * offset;

'g' は Scroller で移動したい要素の ID です。

=ライアン ryan@adobe.com

于 2009-09-01T06:22:32.720 に答える
1

Flex SDK を確認してください。ここに spark.components.List メソッドがあります。DataGroup に同じコードを使用するだけです。

public function ensureIndexIsVisible(index:int):void
{
    if (!layout)
        return;

    var spDelta:Point = dataGroup.layout.getScrollPositionDeltaToElement(index);

    if (spDelta)
    {
        dataGroup.horizontalScrollPosition += spDelta.x;
        dataGroup.verticalScrollPosition += spDelta.y;
    }
}
于 2011-04-26T16:32:33.013 に答える
0

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)]);
   }
于 2012-10-17T16:29:50.973 に答える
0

いくつかの追加の考慮事項:

  1. 私のデータグループのアイテムは一定の高さではないため、その場合、スクローラーを設定するためのより正確な参照は次のようになります。

    var y:int = group.getElementAt(index).y; scroller.viewport.verticalScrollPosition = y;

  2. データグループが仮想化を使用するように設定されていないことを確認してください。私の場合、実行時に要素が追加/削除されていたため、エラーが発生しました。

于 2010-06-03T23:12:42.713 に答える