Hi friends,
I am developing a mobile project in flex4.6. I am using List Component in my project. i need to scroll the list at runtime using script. For Example, if i give index number=5, the list automatically has to scroll to the 5th index position.I used the below code, but it's working well sometimes only.
ActionScript Code:
scrollToIndex(theList,5);
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;
}
}
}
Please help me.