我正在使用 Actionscript 在 s:List 组件中设置选定元素,它可以工作,但 List 不会滚动到选定项目——需要使用滚动条或鼠标滚动。是否可以自动滚动到所选项目?谢谢 !
12 回答
尝试s:List
方法ensureIndexIsVisible(index:int):void。
对于火花:
list.ensureIndexIsVisible(index);
此功能将滚动到 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);
}
这对我有用。不得不使用callLater。
list.selectedItem = "MyTestItem"; //or list.selectedIndex = 10;
this.callLater(updateIndex); //dispatch an update to list
private function updateIndex():void {
list.ensureIndexIsVisible(list.selectedIndex);
}
在 flex-3 中有一个scrollToIndex
方法,因此你可以调用
list.scrollToIndex(list.selectedIndex);
我相信这也应该在 flex-4 中工作。
您可能希望直接访问列表的滚动条并执行以下操作:
list.scroller.scrollRect.y = list.itemRenderer.height * index;
我在这里看到了这个基本想法...... 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();
}
您可以将元素的高度乘以其索引并将此值传递给:
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 和目标索引之间的比较。这修改了组的“horizontalScrollPosition”。
这允许单独的控件基本上充当滚动条,但我需要滑动控件以查看所选项目。我相信这种技术也可以用于自动选择项目