Created
February 26, 2011 02:04
-
-
Save destroytoday/844850 to your computer and use it in GitHub Desktop.
REALLY scrappy prototype of a virtual list with variable-height rows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.destroytoday.scratch.virtualvariableheightlist | |
{ | |
import flash.display.Graphics; | |
import flash.display.Sprite; | |
public class List extends Sprite | |
{ | |
protected var colorList:Array = | |
[ | |
0xFF0099, 0x9900FF, 0xFF9900, 0x663399, 0xFF0000, | |
0x990099, 0x00FF00, 0x0000FF, 0x336699, 0x996633 | |
]; | |
protected var heightList:Array = [100, 20, 90, 120, 70, 80, 140, 130, 90, 40]; | |
protected var foundHeightList:Array = []; | |
protected var numItems:int = 10; | |
protected var _position:Number = 0.0; | |
public function List() | |
{ | |
populateAvgItemHeightUsingView(); | |
draw(); | |
} | |
override public function get width():Number | |
{ | |
return 300.0; | |
} | |
override public function get height():Number | |
{ | |
return 400.0; | |
} | |
public function get position():Number | |
{ | |
return _position; | |
} | |
public function set position(value:Number):void | |
{ | |
_position = Math.max(0.0, Math.min(value, 1.0)); | |
draw(); | |
} | |
protected function get avgItemHeight():Number | |
{ | |
var totalHeight:Number = 0.0; | |
var n:int; | |
for each (var itemHeight:Number in foundHeightList) | |
{ | |
if (!isNaN(itemHeight)) | |
{ | |
totalHeight += itemHeight; | |
} | |
n++; | |
} | |
return totalHeight / n; | |
} | |
protected function populateAvgItemHeightUsingView():void | |
{ | |
var totalHeight:Number = 0.0; | |
var i:int; | |
while (totalHeight < height) | |
{ | |
totalHeight += getItemHeight(i); | |
i++; | |
} | |
} | |
protected function getItemHeight(i:int):Number | |
{ | |
if (i >= foundHeightList.length || !foundHeightList[i]) | |
foundHeightList[i] = heightList[i]; | |
return foundHeightList[i]; | |
} | |
public function draw():void | |
{ | |
var graphics:Graphics = this.graphics; | |
var numVisibleItems:int = height / avgItemHeight; | |
var numInvisibleItems:int = numItems - numVisibleItems; | |
var topRow:int = int(position * numInvisibleItems); | |
var topRowHeight:Number = getItemHeight(topRow); | |
var topItemY:Number = -topRowHeight * ((numInvisibleItems * position) - topRow); | |
var m:uint = numVisibleItems + 2; | |
graphics.clear(); | |
for (var i:uint = 0; i < m; i++) | |
{ | |
var n:uint = topRow + i; | |
if (n >= 0 && n < numItems) | |
{ | |
var itemY:Number = (isNaN(itemY)) ? topItemY : itemY + itemHeight; | |
var itemHeight:Number = getItemHeight(n); | |
var itemColor:uint = colorList[n]; | |
graphics.beginFill(itemColor); | |
graphics.drawRect(0.0, itemY, width, itemHeight); | |
graphics.endFill(); | |
} | |
} | |
graphics.lineStyle(0, 0x000000); | |
graphics.drawRect(0.0, 0.0, 300.0, height); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.destroytoday.scratch.virtualvariableheightlist | |
{ | |
import flash.display.Sprite; | |
import flash.display.StageAlign; | |
import flash.display.StageScaleMode; | |
import flash.events.MouseEvent; | |
public class VirtualVariableHeightListTest extends Sprite | |
{ | |
public var list:List; | |
public function VirtualVariableHeightListTest() | |
{ | |
stage.align = StageAlign.TOP_LEFT; | |
stage.scaleMode = StageScaleMode.NO_SCALE; | |
list = addChild(new List()) as List; | |
list.x = 40.0; | |
list.y = 40.0; | |
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); | |
} | |
protected function mouseMoveHandler(event:MouseEvent):void | |
{ | |
list.position = (stage.mouseY - list.y) / list.height; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment