我使用 Actionscript 在 s:List 组件中设置所选元素,它可以工作,但列表不会滚动到所选项目 - 需要使用滚动条或鼠标滚动。是否可以自动滚动到所选项目?谢谢 !

有帮助吗?

解决方案

其他提示

对于Spark:

<代码> list.ensureIndexIsVisible(索引);

此功能将滚动到Flex 4+列表的顶部。它考虑了项目的高度,因此它适用于具有不同高度的不同项目的列表。

private function scrollToIndex(list:List,index:int):void
{
    if (!list.layout)
        return;

    var dataGroup:DataGroup = list.dataGroup;

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

    if (spDelta)
    {
        dataGroup.horizontalScrollPosition += spDelta.x;
        //move it to the top if the list has enough items
        if(spDelta.y > 0)
        {
            var maxVSP:Number = dataGroup.contentHeight - dataGroup.height;
            var itemBounds:Rectangle = list.layout.getElementBounds(index);
            var newHeight:Number = dataGroup.verticalScrollPosition + spDelta.y 
            + dataGroup.height - itemBounds.height;
            dataGroup.verticalScrollPosition = Math.min(maxVSP, newHeight);
        }
        else
        {
            dataGroup.verticalScrollPosition += spDelta.y;

        }
    }
}
//try this
this.callLater(updateIndex);//where you want to set the selectedIndex

private function updateIndex():void
{
    list.selectedIndex = newIndex;
    list.ensureIndexIsVisible(newIndex);
}

在flex-3中有一个 scrollToIndex 方法,因此你可以调用

list.scrollToIndex(list.selectedIndex);

我相信这也适用于flex-4。

这对我有用。必须使用callLater。

list.selectedItem = "MyTestItem"; //or list.selectedIndex = 10;
this.callLater(updateIndex); //dispatch an update to list

private function updateIndex():void {
    list.ensureIndexIsVisible(list.selectedIndex);
}

我在这里看到了这个基本想法...... http://arthurnn.com/blog/2011/ 1月12日/ CoverFlow的布局换柔性-4 /

public function scrollGroup( n : int ) : void
{
    var scrollPoint : Point = theList.layout.getScrollPositionDeltaToElement( n );
    var duration : Number = ( Math.max( scrollPoint.x, theList.layout.target.horizontalScrollPosition ) - Math.min( scrollPoint.x, theList.layout.target.horizontalScrollPosition )) * .01;
    Tweener.addTween(theList.layout,{ horizontalScrollPosition: scrollPoint.x , time:duration});
}
protected function theList_caretChangeHandler(event:IndexChangeEvent):void
{
    scrollGroup( event.newIndex );
    event.target.invalidateDisplayList();
}

您可能希望直接访问List的滚动条并执行以下操作:

list.scroller.scrollRect.y = list.itemRenderer.height * index;

您可以将元素的高度乘以其索引,并将此值传递给:

yourListID.scroller.viewport.verticalScrollPosition

此自定义列表组件扩展适用于我:

<s:List
    xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    valueCommit="callLater(ensureIndexIsVisible, [selectedIndex])">
</s:List>

我最近在我的一个项目中通过为组中的项目定义了尺寸来实现这一目标。

<s:Scroller x="940" y="0" maxHeight="465" maxWidth="940" horizontalScrollPolicy="off" verticalScrollPolicy="off">
  <s:HGroup  id="tutPane" columnWidth="940" variableColumnWidth="false" gap="0" x="0" y="0">
  </s:HGroup>
</s:Scroller>

接下来,我的按钮控件通过增加私有“targetindex”变量来进行操作,然后我调用了 checkAnimation 函数,该函数使用 Animate 类,与 SimpleMotionPath 以及 tutpane.firstIndexInView 和目标索引之间的比较相结合。这修改了该组的“horizo​​ntalScrollPosition”。

这允许单独的控件本质上充当滚动条,但我需要滑动控件来查看所选项目。我相信这种技术也适用于自动选择项目

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top