Scroll to selected item in Flex 4 Spark List component

前端 未结 12 1459
感动是毒
感动是毒 2020-12-15 17:46

I\'m setting selected element in s:List component with Actionscript, it works, but List doesn\'t scroll to selected item -- need to scroll with scrollbar or mouse. Is it pos

12条回答
  •  囚心锁ツ
    2020-12-15 18:11

    This function will scroll to the top of the list in Flex 4+. It takes in account the height of the item, so it will work for lists with different items with different height.

    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;
    
            }
        }
    }
    

提交回复
热议问题