Return to Snippet

Revision: 48786
at July 9, 2011 00:46 by mcorlan


Initial Code
override public function updateDisplayList(containerWidth:Number, containerHeight:Number):void {
	if (useVirtualLayout)
		updateVirtual(containerWidth, containerHeight);
	else
		updateNonVirtual(containerWidth, containerHeight);
	
}

/**
 * Lay down the current items in the view - this is used when useVirtualLayout is set to true
 */
private function updateVirtual(containerWidth:Number, containerHeight:Number):void {
	var layoutTarget:GroupBase = target;
	if (!(layoutTarget as DataGroup).dataProvider || (layoutTarget as DataGroup).dataProvider.length == 0)
		return;
	
	if (!_containerWidth)
		_containerWidth = containerWidth;
	if (!_containerHeight)
		_containerHeight = containerHeight;
	//a resize of the component occured
	if (_containerWidth != containerWidth || _containerHeight != containerHeight) {
		_containerWidth = containerWidth;
		_containerHeight = containerHeight;
		addExtraItems = 0;
		measure();
		//set the new _firstIndex and _lastIndex
		scrollPositionChanged();		
	}
	trace(layoutTarget.numElements);
	var x:Number = 0;
	var y:Number = 0;
	var maxWidth:Number = 0;
	var maxHeight:Number = 0;
	var elementWidth:Number, elementHeight:Number, prevElementHeight:Number;
	
	//provide the initial values
	if (!_firstIndexInView) 
		_firstIndexInView = 0;
	if (!_lastIndexInView) 
		_lastIndexInView = yToIndex.length - 1 > layoutTarget.height ? yToIndex[layoutTarget.height + 1]  : layoutTarget.numElements - 1;
	
	//add some extra rows after the current view
	currentFirstIndex = _firstIndexInView;
	if (currentFirstIndex < 0 )
		currentFirstIndex = 0;
	if (!addExtraItems) {
		addExtraItems = Math.ceil(containerWidth / (_columnWidth + _horizontalGap)) * Math.ceil(containerHeight / ((_tileHeight + _sectionHeight) / 2)); 
	}
	currentLastIndex = _firstIndexInView + addExtraItems;
	if (currentLastIndex > layoutTarget.numElements - 1)
		currentLastIndex = layoutTarget.numElements - 1;
	
	y = indexToY[currentFirstIndex];
	var count:int = currentLastIndex + 1;
	var element:ILayoutElement;
	
	for (var i:int = currentFirstIndex; i < count; i++) {
		// get the current element, we're going to work with the
		// ILayoutElement interface
		element = layoutTarget.getVirtualElementAt(i);
		// Resize the element to its preferred size by passing
		// NaN for the width and height constraints
		element.setLayoutBoundsSize(NaN, NaN);
		if (element["data"] && _sectionLabel in element["data"]) {
			elementWidth = containerWidth;
			elementHeight = _sectionHeight;
		} else {
			elementWidth = _columnWidth;
			elementHeight = _tileHeight;
		}				
		element.setLayoutBoundsSize(elementWidth, elementHeight);
		
		// Would the element fit on this line, or should we move
		// to the next line?
		if (x + elementWidth > containerWidth) {
			x = 0;
			//move to the next row
			y += prevElementHeight + _verticalGap;
		}
		// Position the element
		element.setLayoutBoundsPosition(x, y);
		prevElementHeight = elementHeight;
		// Update the current position, add the gap
		x += elementWidth + _horizontalGap;
	}
}

Initial URL
http://corlan.org/?p=2987

Initial Description

                                

Initial Title
Custom Layout manager updateDisplayList() method

Initial Tags

                                

Initial Language
ActionScript 3