Apache flex 以编程方式更改滚动条容器的位置(通过HGroup索引)

Apache flex 以编程方式更改滚动条容器的位置(通过HGroup索引),apache-flex,actionscript,flex4,flash-builder,Apache Flex,Actionscript,Flex4,Flash Builder,我试图更改事件上滚动条容器的可见索引。滚动条包含一个带有图像的HGroup,因此在一个事件中,我希望通过指定HGroup索引来更改滚动条中可见的图像。这可能吗 集装箱代码: <s:Scroller id="test" includeIn="startState" x="53" y="20" width="170" height="200" depth="2" scrollSnappingMode="leadingEdge"> <s:HGroup

我试图更改事件上滚动条容器的可见索引。滚动条包含一个带有图像的HGroup,因此在一个事件中,我希望通过指定HGroup索引来更改滚动条中可见的图像。这可能吗

集装箱代码:

<s:Scroller id="test" includeIn="startState" x="53" y="20" width="170"
            height="200" depth="2" scrollSnappingMode="leadingEdge">
    <s:HGroup gap="0" width="170" height="200">
        <s:Image id="BrigadeEmblem1" width="170" height="200" smooth="true"
                 smoothingQuality="high" source="assets/1st Stryker Brigade.png"
                 verticalAlign="middle"/>
        <s:Image id="BrigadeEmblem4" width="170" height="200" smooth="true"
                 smoothingQuality="high" source="assets/4th Stryker Brigade.png"
                 verticalAlign="middle"/>
    </s:HGroup>
</s:Scroller>

例如,如果“BrigadeDegma1”在滚动条中可见,我想在听到特定事件时,通过编程将可见图像更改为“BrigadeDegma4”

正是这样

    setTimeout(image1,3000); // in 3 seconds this event will call for the function(image1)

    function image1(){
    myMovieClip .visible = false;
    }

是的,实际上这是可能的

我扩展了Scroller类并创建了以下方法:

public function ensureIndexIsVisible(index:int):void {

        if (!viewport || !(viewport is GroupBase) || !(viewport as GroupBase).layout) {
            return;
        }

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

        // if spDelta is null, no scrolling is required.
        if (spDelta) {
            GroupBase(viewport).horizontalScrollPosition += spDelta.x;
            GroupBase(viewport).verticalScrollPosition += spDelta.y;
        }
    }
因此,当您想要第二张图像时:

myScroller.ensureIndexIsVisible(1);

现在回答有点晚,但我希望这对某些人有用。我已经编写了一些代码,可以自动滚动
滚动条
,使聚焦组件完全可见。你可以用它来解决你的问题

 private var _focusedComponentPaddingTop:int = 10;
 private var _focusedComponentPaddingBottom:int = 10;
 private var _focusedComponentPaddingLeft:int = 5;
 private var _focusedComponentPaddingRight:int = 5;

 public function makeFocusedItemVisible(event:FocusEvent):void {
    // Target is the actual object that has focus.
    var target:DisplayObject = DisplayObject(event.target);

    if (target != null && contains(target)) {
       // The container's viewable area
       var visibleArea:Rectangle = getVisibleArea();
       var changed:Boolean = false;

       // Calculate the position of the target in the container.
       var topLeft:Point = new Point(0, 0);
       topLeft = target.localToGlobal(topLeft);
       topLeft = globalToLocal(topLeft);

       var bottomRight:Point =
             new Point(target.width, target.height);
       bottomRight = target.localToGlobal(bottomRight);
       bottomRight = globalToLocal(bottomRight);

       // Check if the component is visible and move the scrollbars if not
       if (bottomRight.x > visibleArea.right) {
          var deltaX:Number = bottomRight.x - visibleArea.right;
          viewport.horizontalScrollPosition += deltaX + _focusedComponentPaddingRight;
          topLeft.x -= deltaX;
          changed = true;
       }

       if (topLeft.x < visibleArea.left) {
          viewport.horizontalScrollPosition -=
                visibleArea.left - topLeft.x + _focusedComponentPaddingLeft;
          changed = true;
       }

       if (bottomRight.y > visibleArea.bottom) {
          var deltaY:Number = bottomRight.y - visibleArea.bottom;
          viewport.verticalScrollPosition += deltaY + focusedComponentPaddingBottom;
          topLeft.y -= deltaY;
          changed = true;
       }

       if (topLeft.y < visibleArea.top) {
          viewport.verticalScrollPosition -=
                visibleArea.top - topLeft.y + focusedComponentPaddingTop;
          changed = true;
       }

       // Call validateNow() to get the container move the component 
       if (changed) {
          validateNow();
       }
    }
 }

 private function getVisibleArea():Rectangle {
    var area:Rectangle = new Rectangle();

    area.x = x;
    area.y = y;
    area.width = width;
    area.height = height;

    return area;
 }
private var\u focusedComponentPaddingTop:int=10;
private var_focusedComponent padding bottom:int=10;
private var_focusedcomponent padding left:int=5;
private var_focusedComponentPaddingRight:int=5;
公共函数makeFocusedItemVisible(事件:FocusEvent):无效{
//目标是具有焦点的实际对象。
变量目标:DisplayObject=DisplayObject(event.target);
if(目标!=null&&contains(目标)){
//容器的可视区域
var visibleArea:Rectangle=getVisibleArea();
变量已更改:布尔值=false;
//计算目标在容器中的位置。
变量左上角:点=新点(0,0);
topLeft=target.localToGlobal(topLeft);
左上=全局全局全局(左上);
右下角:点=
新点(target.width、target.height);
bottomRight=target.localToGlobal(bottomRight);
bottomRight=全局颜色(bottomRight);
//检查组件是否可见,如果不可见,则移动滚动条
如果(右下角.x>可视区域右下角){
var deltaX:Number=bottomRight.x-visibleArea.right;
viewport.horizontalScrollPosition+=deltaX+\u focusedComponentPaddingRight;
左上角.x-=deltaX;
更改=正确;
}
if(左上角x可视区域底部){
var deltaY:Number=bottomRight.y-visibleArea.bottom;
viewport.verticalScrollPosition+=deltaY+focusedComponentPaddingBottom;
左上角y-=三角洲;
更改=正确;
}
if(左上角y<可视区域顶部){
viewport.verticalScrollPosition-=
visibleArea.top-topLeft.y+聚焦组件填充顶部;
更改=正确;
}
//调用validateNow()以使容器移动组件
如果(更改){
validateNow();
}
}
}
私有函数getVisibleArea():矩形{
变量区域:矩形=新矩形();
面积x=x;
面积y=y;
面积.宽度=宽度;
面积.高度=高度;
返回区;
}