在Flex 4 Spark List组件中滚动到所选项目

26

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

12个回答

23

谢谢,但似乎只在最新的Flex 4 SDK构建中有效(Flex Builder 4 Beta 2)。我意识到我正在使用一个没有这个方法的旧版本。 - Rodion Bykov
1
这仅向上滚动,以便物品的顶部可见,如果物品很高,则不会滚动到底部。 - JTtheGeek
@JTtheGeek 是的,在Spark List 4.6中,似乎必须点击两次才能滚动到底部。 - Nemi

6

For Spark:

list.ensureIndexIsVisible(index);


4

此函数将在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;

        }
    }
}

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

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

1
更短的版本:callLater(list.ensureIndexIsVisible,[list.selectedIndex]),但仍不是最佳解决方案 - 有时会抛出索引超出范围的错误。 - Alexander Farber

3
在Flex-3中有一个名为scrollToIndex的方法,因此您可以调用它。
list.scrollToIndex(list.selectedIndex);

我相信这在flex-4中也应该可以工作。


很遗憾,Spark List 中没有这样的方法,但 Halo List 组件中有。Flex 4 正在不断发展并且仍处于测试阶段,希望这个问题能够得到解决。 - Rodion Bykov

3

这对我很有帮助。必须使用callLater函数。

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

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

2

我在这里看到了这个基本的想法... http://arthurnn.com/blog/2011/01/12/coverflow-layout-for-flex-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();
}

2
您可能需要直接访问列表的滚动条并执行以下操作: list.scroller.scrollRect.y = list.itemRenderer.height * index;
这将帮助您在列表中快速定位到所需项。

我发现你不能直接更改scrollRect内的值,而是需要使用一个新的矩形来更新scrollRect?就像这样 - eldamar

1
这个自定义列表组件扩展对我很有用:
<s:List
    xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    valueCommit="callLater(ensureIndexIsVisible, [selectedIndex])">
</s:List>

1

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

yourListID.scroller.viewport.verticalScrollPosition

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接