Reputation: 5880
I'm using FB4 and apparently I need to use ensureIndexIsVisible()
to scroll to specific item in my s:List
. Anyway, the code below successfully scrolls to the item but does not scroll it to the top of the list (it's at the bottom, and cut off somewhat). Is there anyway to do that?
MXML:
<s:List id="Schedule" dataProvider="{schedule}" creationComplete="creationCompleteHandler(event)"/>
AS3:
protected function creationCompleteHandler(event:Event):void {
var d:Date = new Date();
var today:String = String((d.month + 1) + "/" + d.date + "/" + d.fullYear);
var dP:XMLListCollection = event.currentTarget.dataProvider;
for(var i:uint; i < dP.length; i++){
if(dP.child("date")[i] == today){
event.currentTarget.ensureIndexIsVisible(i);
}
}
}
Upvotes: 1
Views: 1331
Reputation: 1
Here is a simple one-liner for a Spark List in Flex 4:
list.layout.verticalScrollPosition = list.layout.getElementBounds(list.selectedIndex).y;
Upvotes: 0
Reputation: 329
Was able to work out a simpler solution - posting it here as no-one else seems to have posted something similar online.
The method spark.layouts.supportClasses.LayoutBase#getScrollPositionDeltaToElementHelper allows for a topOffset to be passed in, but the default implementation passes NaN. If create a custom layout that passes "0" as the topOffset, then calling ensureIndexIsVisible on your List will cause the item at that index to be top aligned.
See custom layout class below:
public class ScrollToElementVerticalLayout extends VerticalLayout
{
public override function getScrollPositionDeltaToElement(index:int):Point
{
// pass 0 as the topOffset so the element aligns with the top of the list
return getScrollPositionDeltaToElementHelper(index, 0);
}
}
Upvotes: 1
Reputation: 26
Bad solution, but it's work for me
var pt:Point = list.layout.getScrollPositionDeltaToElement(i);
while (pt) {
list.validateNow();
if (pt.y > 0) {
var delta:int = list.layout.getVerticalScrollPositionDelta(NavigationUnit.DOWN);
} else {
delta = list.layout.getVerticalScrollPositionDelta(NavigationUnit.UP);
}
list.layout.verticalScrollPosition += delta;
pt = list.layout.getScrollPositionDeltaToElement(i);
}
Upvotes: 1