Posted By

mcorlan on 07/09/11


Tagged


Versions (?)

Custom Layout manager updateDisplayList() method


 / Published in: ActionScript 3
 

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

  1. override public function updateDisplayList(containerWidth:Number, containerHeight:Number):void {
  2. if (useVirtualLayout)
  3. updateVirtual(containerWidth, containerHeight);
  4. else
  5. updateNonVirtual(containerWidth, containerHeight);
  6.  
  7. }
  8.  
  9. /**
  10.  * Lay down the current items in the view - this is used when useVirtualLayout is set to true
  11.  */
  12. private function updateVirtual(containerWidth:Number, containerHeight:Number):void {
  13. var layoutTarget:GroupBase = target;
  14. if (!(layoutTarget as DataGroup).dataProvider || (layoutTarget as DataGroup).dataProvider.length == 0)
  15. return;
  16.  
  17. if (!_containerWidth)
  18. _containerWidth = containerWidth;
  19. if (!_containerHeight)
  20. _containerHeight = containerHeight;
  21. //a resize of the component occured
  22. if (_containerWidth != containerWidth || _containerHeight != containerHeight) {
  23. _containerWidth = containerWidth;
  24. _containerHeight = containerHeight;
  25. addExtraItems = 0;
  26. measure();
  27. //set the new _firstIndex and _lastIndex
  28. scrollPositionChanged();
  29. }
  30. trace(layoutTarget.numElements);
  31. var x:Number = 0;
  32. var y:Number = 0;
  33. var maxWidth:Number = 0;
  34. var maxHeight:Number = 0;
  35. var elementWidth:Number, elementHeight:Number, prevElementHeight:Number;
  36.  
  37. //provide the initial values
  38. if (!_firstIndexInView)
  39. _firstIndexInView = 0;
  40. if (!_lastIndexInView)
  41. _lastIndexInView = yToIndex.length - 1 > layoutTarget.height ? yToIndex[layoutTarget.height + 1] : layoutTarget.numElements - 1;
  42.  
  43. //add some extra rows after the current view
  44. currentFirstIndex = _firstIndexInView;
  45. if (currentFirstIndex < 0 )
  46. currentFirstIndex = 0;
  47. if (!addExtraItems) {
  48. addExtraItems = Math.ceil(containerWidth / (_columnWidth + _horizontalGap)) * Math.ceil(containerHeight / ((_tileHeight + _sectionHeight) / 2));
  49. }
  50. currentLastIndex = _firstIndexInView + addExtraItems;
  51. if (currentLastIndex > layoutTarget.numElements - 1)
  52. currentLastIndex = layoutTarget.numElements - 1;
  53.  
  54. y = indexToY[currentFirstIndex];
  55. var count:int = currentLastIndex + 1;
  56. var element:ILayoutElement;
  57.  
  58. for (var i:int = currentFirstIndex; i < count; i++) {
  59. // get the current element, we're going to work with the
  60. // ILayoutElement interface
  61. element = layoutTarget.getVirtualElementAt(i);
  62. // Resize the element to its preferred size by passing
  63. // NaN for the width and height constraints
  64. element.setLayoutBoundsSize(NaN, NaN);
  65. if (element["data"] && _sectionLabel in element["data"]) {
  66. elementWidth = containerWidth;
  67. elementHeight = _sectionHeight;
  68. } else {
  69. elementWidth = _columnWidth;
  70. elementHeight = _tileHeight;
  71. }
  72. element.setLayoutBoundsSize(elementWidth, elementHeight);
  73.  
  74. // Would the element fit on this line, or should we move
  75. // to the next line?
  76. if (x + elementWidth > containerWidth) {
  77. x = 0;
  78. //move to the next row
  79. y += prevElementHeight + _verticalGap;
  80. }
  81. // Position the element
  82. element.setLayoutBoundsPosition(x, y);
  83. prevElementHeight = elementHeight;
  84. // Update the current position, add the gap
  85. x += elementWidth + _horizontalGap;
  86. }
  87. }

Report this snippet  

You need to login to post a comment.