我正在使用Actionscript设置s:List组件中的选定元素,它可以工作,但是List没有滚动到选定的项目 - 需要使用滚动条或鼠标滚动。是否可以自动滚动到选定的项目?谢谢!
我正在使用Actionscript设置s:List组件中的选定元素,它可以工作,但是List没有滚动到选定的项目 - 需要使用滚动条或鼠标滚动。是否可以自动滚动到选定的项目?谢谢!
尝试使用s:List
方法ensureIndexIsVisible(index:int):void。
For Spark:
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.ensureIndexIsVisible,[list.selectedIndex])
,但仍不是最佳解决方案 - 有时会抛出索引超出范围的错误。 - Alexander FarberscrollToIndex
的方法,因此您可以调用它。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/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();
}
list.scroller.scrollRect.y = list.itemRenderer.height * index;
<s:List
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
valueCommit="callLater(ensureIndexIsVisible, [selectedIndex])">
</s:List>
您可以将元素的高度乘以其索引,并将此值传递给:
yourListID.scroller.viewport.verticalScrollPosition