26

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

4

12 回答 12

23

尝试s:List方法ensureIndexIsVisible(index:int):void

于 2009-11-06T20:17:38.307 回答
6

对于火花:

list.ensureIndexIsVisible(index);

于 2010-11-26T19:13:53.953 回答
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;

        }
    }
}
于 2011-11-18T12:18:39.187 回答
4
//try this
this.callLater(updateIndex);//where you want to set the selectedIndex

private function updateIndex():void
{
    list.selectedIndex = newIndex;
    list.ensureIndexIsVisible(newIndex);
}
于 2012-05-22T12:45:12.900 回答
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);
}
于 2013-06-26T23:07:13.397 回答
3

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

list.scrollToIndex(list.selectedIndex);

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

于 2009-10-22T04:42:06.990 回答
2

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

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

于 2009-11-06T20:07:35.567 回答
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();
}
于 2011-08-27T09:06:00.910 回答
1

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

yourListID.scroller.viewport.verticalScrollPosition
于 2009-11-08T06:01:22.790 回答
1

这是一个错误 - 您可以在https://issues.apache.org/jira/browse/FLEX-33660上查看演示和解决方法

于 2013-08-10T10:15:05.723 回答
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>
于 2016-04-11T17:23:59.117 回答
0

我最近在我的一个项目中通过为组中的项目定义大小来实现这一点。

<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”。

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

于 2009-11-17T00:53:27.537 回答