2009-10-21 42 views

回答

23
+0

謝謝,但它似乎只適用於最新的Flex 4 SDK版本(Flex Builder 4 Beta 2)。我意識到我使用了一種沒有這種方法的舊版本。 –

+1

這也只能滾動,使項目的頂部是可見的,如果項目很高,這不會滾動到底部 – JTtheGeek

+0

@JTtheGeek是的,在Spark List 4.6中,似乎您必須單擊兩次以滾動到底部。 – Nemi

3

在柔性-3有一個scrollToIndex方法,因此你可以調用

list.scrollToIndex(list.selectedIndex); 

我相信這應該彎曲-4工作了。

+0

不幸的是,沒有。 Spark List中沒有這種方法,但它是Halo List組件的一部分。 Flex 4正在不斷髮展並仍處於Beta階段,希望這個問題能夠得到解決。 –

2

您可能需要直接訪問列表的滾動條,並做一些事情,如:

list.scroller.scrollRect.y = list.itemRenderer.height * index;

+0

我發現你不能直接更改scrollRect中的值,而是需要使用新的矩形更新scrollRect?像[this](http://www.actionscript.org/forums/showthread.php3?t=190795) – eldamar

1

您可以通過它的索引乘以元素的高度,這個值傳遞給:

yourListID.scroller.viewport.verticalScrollPosition 
0

我最近有我的組中的項目定義的大小來實現這在我的項目之一..

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

以下的操作通過增加私人「targetindex」這個工作我的按鈕控制變量,然後我叫checkAnimation功能,它使用了動畫類,與SimpleMotionPath和tutpane.firstIndexInView之間的對比組合TARG et指數。這修改了該組的「horizo​​ntalScrollPosition」。

這使得獨立控制基本上充當一個滾動條,但我有滑動控制的要求,查看所選擇的項目。我相信這種技術可以爲項目的自動選擇,以及

6

工作星火:

list.ensureIndexIsVisible(index);

2

我在這裏看到這個基本的想法... 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(); 
} 
4

此功能將滾動到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; 

     } 
    } 
} 
4
//try this 
this.callLater(updateIndex);//where you want to set the selectedIndex 

private function updateIndex():void 
{ 
    list.selectedIndex = newIndex; 
    list.ensureIndexIsVisible(newIndex); 
} 
+1

較短的版本:'callLater(list.ensureIndexIsVisible,[list.selectedIndex])'但仍然不是最好的解決方案 - 有時拋出索引超出範圍錯誤 –

2

這爲我工作。不得不使用callLater。

list.selectedItem = "MyTestItem"; //or list.selectedIndex = 10; 
this.callLater(updateIndex); //dispatch an update to list 

private function updateIndex():void { 
    list.ensureIndexIsVisible(list.selectedIndex); 
} 
1

這個自定義列表組件擴展爲我工作:

<s:List 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    valueCommit="callLater(ensureIndexIsVisible, [selectedIndex])"> 
</s:List>