flex/spark" xmlns:local="*" width="440" viewSourceURL="srcview/index.html"> height="116" useVirtualLayout="false" dataProvider="{createItems(20)}">height="116" useVirtualLayout="true" dataProvider="{createItems(1000)}">
.components.supportClasses.GroupBase; importspark.core.NavigationUnit; importspark.layouts.supportClasses.LayoutBase; publicclassSimpleHorizontalLayoutextendsLayoutBase{privatevarminEligibleScrollPosition:Number= 0; privatevarmaxEligibleScrollPosition:Number= 0; /** * Called when the user clicks on the left scroll button or the left part of the track of the scrollbar */overrideprotectedfunctiongetElementBoundsLeftOfScrollRect(scrollRect:Rectangle):Rectangle{varoffset:Number=horizontalScrollPosition%typicalLayoutElement.getLayoutBoundsWidth(); varwidth:Number=typicalLayoutElement.getLayoutBoundsWidth(); varheight:Number=typicalLayoutElement.getLayoutBoundsHeight(); returnnewRectangle(scrollRect.x-width+offset,scrollRect.y,width,height); }/** * Called when the user clicks on the right scroll button or the right part of the track of the scrollbar */overrideprotectedfunctiongetElementBoundsRightOfScrollRect(scrollRect:Rectangle/** * TODO: This works with getNavigationDestinationIndex() for keyboard scrolling a List based target. * * Given this layout's assumptions this method easily handles both the virtual and non-virtual case * since an element's position can be calculated solely by its index and the typicalLayoutElement width * */overridepublicfunctiongetElementBounds(index:int):Rectangle{if(!target)returnnull; if((index=target.numElements))returnnull; vareltX:Number=typicalLayoutElement.getLayoutBoundsWidth()*index; vareltY:Number= 0 vareltW:Number=typicalLayoutElement.getLayoutBoundsWidth()vareltH:Number=typicalLayoutElement.getLayoutBoundsHeight(); returnnewRectangle(eltX,eltY,eltW,eltH); }/** * * This method is used by subclasses of ListBase to handle keyboard navigation. Given a current index * (the index that is currently focused) this method returns the index to navigate to given a keyboard * navigation command like LEFT/RIGHT/HOME/END. * */overridepublicfunctiongetNavigationDestinationIndex(currentIndex:int,navigationUnit:uint,arrowKeysWrapFocus:Boolean):int{if(!target||target.numElementsHeight and * measuredMinWidth,Height. * * This method does not get layout elements from the target except * as a side effect of calling typicalLayoutElement. * * It assumes all elements are of equal size and equal to the size of the * typicalLayoutElement. */privatefunctionmeasureVirtual(layoutTarget:GroupBase):void{vareltCount:uint=layoutTarget.numElements; layoutTarget.measuredWidth=(eltCount*typicalLayoutElement.getLayoutBoundsWidth()); layoutTarget.measuredHeight=typicalLayoutElement.getLayoutBoundsHeight(); layoutTarget.measuredMinWidth=layoutTarget.measuredWidth; layoutTarget.measuredMinHeight=layoutTarget.measuredHeight; }/** * Compute exact values for measuredWidth,Height and measuredMinWidth,Height. * * Measure each of the layout elements. We then only * consider the height of the elements remaining. */privatefunctionmeasureReal(layoutTarget:GroupBase)typicalLayoutElement)return; if(target.numElementsheight constraints element.setLayoutBoundsSize(NaN,NaN); // find the size of the element varelementWidth:Number=element.getLayoutBoundsWidth(); varelementHeight:Number=element.getLayoutBoundsHeight(); // update the target's contentWidth and contentHeight target.setContentSize(Math.ceil(typicalLayoutElement.getLayoutBoundsWidth()*target.numElements),Math.ceil(Math.max(elementHeight,target.contentHeight))); // update the x position for where to place the next element x+=elementWidth; }// // Step 7: Keep track of the extent of the renderers that are partially in view // // ie: how much they stick out of view. Keeping track of that allows us to call // invalidateDisplayList() less in scrollPositionChanged(). // minEligibleScrollPosition=target.horizontalScrollPosition; maxEligibleScrollPosition=minEligibleScrollPosition+target.width; // now subtract the left offset minEligibleScrollPosition-=firstIndexInViewOffset; // and add the right offset maxEligibleScrollPosition+=typicalLayoutElement.getLayoutBoundsWidth()-firstIndexInViewOffset; }/** * Gets called by updateDisplayList when useVirtualLayout = false */privatefunctionupdateDisplayListReal(containerWidth:Number,containerHeight:Number):void{// the x value to position the next element at varx:Number= 0; // the y value to position the next element at vary:Number= 0; // loop through every element (even those not in view) for(vark:int= 0; kheight constraints element.setLayoutBoundsSize(NaN,NaN); // find the size of the element varelementWidth:Number=element.getLayoutBoundsWidth(); varelementHeight:Number=element.getLayoutBoundsHeight(); // update the layoutTarget's contentWidth and contentHeight target.setContentSize(Math.ceil(x+elementWidth),Math.ceil(Math.max(elementHeight,target.contentHeight))); // update the x position for where to place the next element x+=elementWidth; }}/** * In a virtual layout you need to invalidate the display list if the user has * scrolled by a large enough distance to expose items that don't currently * have renderers created. We keep track of this range of scroll positions * using minEligibleScrollPosition/maxEligibleScrollPositio n. */overrideprotectedfunctionscrollPositionChanged():void{super.scrollPositionChanged(); if(!target)return; if(useVirtualLayout){// an optimization: only invalidate the display list if the scroll position // has changed enough that new items are coming into view if(horizontalScrollPositionmaxEligibleScrollPosition-target.width){target.invalidateDisplayList(); }}}}}
flex/spark" width="50" height="100">
.components.supportClasses.GroupBase; importspark.core.NavigationUnit; importspark.layouts.supportClasses.LayoutBase; publicclassSimpleHorizontalLayoutextendsLayoutBase{privatevarminEligibleScrollPosition:Number= 0; privatevarmaxEligibleScrollPosition:Number= 0; /** * Called when the user clicks on the left scroll button or the left part of the track of the scrollbar */overrideprotectedfunctiongetElementBoundsLeftOfScrollRect(scrollRect:Rectangle):Rectangle{varoffset:Number=horizontalScrollPosition%typicalLayoutElement.getLayoutBoundsWidth(); varwidth:Number=typicalLayoutElement.getLayoutBoundsWidth(); varheight:Number=typicalLayoutElement.getLayoutBoundsHeight(); returnnewRectangle(scrollRect.x-width+offset,scrollRect.y,width,height); }/** * Called when the user clicks on the right scroll button or the right part of the track of the scrollbar */overrideprotectedfunctiongetElementBoundsRightOfScrollRect(scrollRect:Rectangle/** * TODO: This works with getNavigationDestinationIndex() for keyboard scrolling a List based target. * * Given this layout's assumptions this method easily handles both the virtual and non-virtual case * since an element's position can be calculated solely by its index and the typicalLayoutElement width * */overridepublicfunctiongetElementBounds(index:int):Rectangle{if(!target)returnnull; if((index=target.numElements))returnnull; vareltX:Number=typicalLayoutElement.getLayoutBoundsWidth()*index; vareltY:Number= 0 vareltW:Number=typicalLayoutElement.getLayoutBoundsWidth()vareltH:Number=typicalLayoutElement.getLayoutBoundsHeight(); returnnewRectangle(eltX,eltY,eltW,eltH); }/** * * This method is used by subclasses of ListBase to handle keyboard navigation. Given a current index * (the index that is currently focused) this method returns the index to navigate to given a keyboard * navigation command like LEFT/RIGHT/HOME/END. * */overridepublicfunctiongetNavigationDestinationIndex(currentIndex:int,navigationUnit:uint,arrowKeysWrapFocus:Boolean):int{if(!target||target.numElementsHeight and * measuredMinWidth,Height. * * This method does not get layout elements from the target except * as a side effect of calling typicalLayoutElement. * * It assumes all elements are of equal size and equal to the size of the * typicalLayoutElement. */privatefunctionmeasureVirtual(layoutTarget:GroupBase):void{vareltCount:uint=layoutTarget.numElements; layoutTarget.measuredWidth=(eltCount*typicalLayoutElement.getLayoutBoundsWidth()); layoutTarget.measuredHeight=typicalLayoutElement.getLayoutBoundsHeight(); layoutTarget.measuredMinWidth=layoutTarget.measuredWidth; layoutTarget.measuredMinHeight=layoutTarget.measuredHeight; }/** * Compute exact values for measuredWidth,Height and measuredMinWidth,Height. * * Measure each of the layout elements. We then only * consider the height of the elements remaining. */privatefunctionmeasureReal(layoutTarget:GroupBase)typicalLayoutElement)return; if(target.numElementsheight constraints element.setLayoutBoundsSize(NaN,NaN); // find the size of the element varelementWidth:Number=element.getLayoutBoundsWidth(); varelementHeight:Number=element.getLayoutBoundsHeight(); // update the target's contentWidth and contentHeight target.setContentSize(Math.ceil(typicalLayoutElement.getLayoutBoundsWidth()*target.numElements),Math.ceil(Math.max(elementHeight,target.contentHeight))); // update the x position for where to place the next element x+=elementWidth; }// // Step 7: Keep track of the extent of the renderers that are partially in view // // ie: how much they stick out of view. Keeping track of that allows us to call // invalidateDisplayList() less in scrollPositionChanged(). // minEligibleScrollPosition=target.horizontalScrollPosition; maxEligibleScrollPosition=minEligibleScrollPosition+target.width; // now subtract the left offset minEligibleScrollPosition-=firstIndexInViewOffset; // and add the right offset maxEligibleScrollPosition+=typicalLayoutElement.getLayoutBoundsWidth()-firstIndexInViewOffset; }/** * Gets called by updateDisplayList when useVirtualLayout = false */privatefunctionupdateDisplayListReal(containerWidth:Number,containerHeight:Number):void{// the x value to position the next element at varx:Number= 0; // the y value to position the next element at vary:Number= 0; // loop through every element (even those not in view) for(vark:int= 0; kheight constraints element.setLayoutBoundsSize(NaN,NaN); // find the size of the element varelementWidth:Number=element.getLayoutBoundsWidth(); varelementHeight:Number=element.getLayoutBoundsHeight(); // update the layoutTarget's contentWidth and contentHeight target.setContentSize(Math.ceil(x+elementWidth),Math.ceil(Math.max(elementHeight,target.contentHeight))); // update the x position for where to place the next element x+=elementWidth; }}/** * In a virtual layout you need to invalidate the display list if the user has * scrolled by a large enough distance to expose items that don't currently * have renderers created. We keep track of this range of scroll positions * using minEligibleScrollPosition/maxEligibleScrollPositio n. */overrideprotectedfunctionscrollPositionChanged():void{super.scrollPositionChanged(); if(!target)return; if(useVirtualLayout){// an optimization: only invalidate the display list if the scroll position // has changed enough that new items are coming into view if(horizontalScrollPositionmaxEligibleScrollPosition-target.width){target.invalidateDisplayList(); }}}}}
flex/spark" width="50" height="100">