2009-08-28 1 views
2

私はspark Scroller内に多くの子テキスト入力を持っています。どのようにしてidが "x"のTextInputにフォーカスが入るようにすることができますか?スクロールバーがその子アイテムに自動的にスクロールするようにするには?FLEX 4 s:Scroller、含まれているコンポーネントを表示する方法は?

私は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> 

おかげで、フィリップ

答えて

2

理由は、それが実際にスクロールバーのscrollPositionを変更動かない、は、setFocusはちょうどオブジェクトがアクティブになることです。リストのようなもっと複雑なクラスでは、それはより単純ですが、Scrollerはかなり基本的なので少し厄介です。

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

「G」は、あなたのスクローラ中に移動したい要素のidです
var index:Number = group.getElementIndex(g); 
var offset:Number = group.getElementAt(index).height; 
scroller.viewport.verticalScrollPosition = index * offset; 

=ライアン[email protected]

0

カップル追加の考慮事項:、より正確な基準を設定する場合厥ので、もし私のデータグループにおける

  1. 彼のアイテムは一定の高さではありません以下のようになります。

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

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

1

ただ、自分のデータグループのために同じコードを使用し、ここでspark.components.List方法だと、Flex SDKをチェックアウト:

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; 
    } 
} 
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)]); 
    } 
関連する問題