Scroll to selected item in Flex 4 Spark List component

前端 未结 12 1463
感动是毒
感动是毒 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:08

    I recently accomplished this in one of my projects by having a defined size for my items in the group..

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

    Following this my button controls for manipulation worked by incrementing a private "targetindex" variable, then I called a checkAnimation function, which used the Animate class, in combo with a SimpleMotionPath and a comparison between tutpane.firstIndexInView and target index. This modified the "horizontalScrollPosition" of the group.

    This allowed separate controls to essentially act as a scroll bar, but I had the requirement of sliding the control to view the selected item.. I believe this technique could work for automated selection of items as well

    0 讨论(0)
  • 2020-12-15 18:10

    I saw this basic idea here... 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();
    }
    
    0 讨论(0)
  • 2020-12-15 18:11

    Try the s:List method ensureIndexIsVisible(index:int):void.

    0 讨论(0)
  • 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;
    
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-15 18:14

    You can multiply the height of an element by its index and pass this value to:

    yourListID.scroller.viewport.verticalScrollPosition
    
    0 讨论(0)
  • 2020-12-15 18:19

    For Spark:

    list.ensureIndexIsVisible(index);

    0 讨论(0)
提交回复
热议问题