package
{
import flash.events.Event;
import flash.events.EventDispatcher;
import mx.collections.IList;
import mx.collections.errors.ItemPendingError;
import mx.events.CollectionEvent;
import mx.events.CollectionEventKind;
import mx.events.PropertyChangeEvent;
import mx.events.PropertyChangeEventKind;
import mx.resources.IResourceManager;
import mx.resources.ResourceManager;
import mx.rpc.IResponder;
[Event(name="collectionChange", type="mx.events.CollectionEvent")]
public class LazyList extends EventDispatcher implements IList
{
/**
* @private
*/
private static function get resourceManager():IResourceManager
{
return ResourceManager.getInstance();
}
/**
* @private
*/
private static function checkItemIndex(index:int, listLength:int):void
{
if (index < 0 || (index >= listLength))
{
const message:String = resourceManager.getString("collections", "outOfBounds", [ index ]);
throw new RangeError(message);
}
}
/**
* @private
* The IList's items.
*/
private const data:Vector.<*> = new Vector.<*>();
/**
* Construct a PagedList with the specified length and pageSize.
*/
public function PagedList(length:int=1000, pageSize:int=10)
{
this.data.length = length;
this.pageSize = pageSize;
for (var i:int = 0; i < data.length; i++)
data[i] = undefined;
}
//----------------------------------
// loadItemsFunction
//----------------------------------
private var _loadItemsFunction:Function = null;
public function get loadItemsFunction():Function
{
return _loadItemsFunction;
}
/**
* @private
*/
public function set loadItemsFunction(value:Function):void
{
_loadItemsFunction = value;
}
//----------------------------------
// length
//----------------------------------
[Bindable("collectionChange")]
public function get length():int
{
return data.length;
}
/**
* @private
*/
public function set length(value:int):void
{
const oldLength:int = data.length;
const newLength:int = value;
if (oldLength == newLength)
return;
var ce:CollectionEvent = null;
if (hasEventListener(CollectionEvent.COLLECTION_CHANGE))
ce = new CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
if (oldLength < newLength)
{
if (ce)
{
ce.location = Math.max(oldLength - 1, 0);
ce.kind = CollectionEventKind.ADD;
const itemsLength:int = newLength - oldLength;
for (var i:int = 0; i < itemsLength; i++)
ce.items.push(undefined);
}
data.length = newLength;
for (var newIndex:int = Math.max(oldLength - 1, 0); newIndex < newLength; newIndex++)
data[newIndex] = undefined;
}
else // oldLength > newLength
{
if (ce)
{
ce.location = Math.max(newLength - 1, 0);
ce.kind = CollectionEventKind.REMOVE;
for (var oldIndex:int = Math.max(newLength - 1, 0); oldIndex < oldLength; oldIndex++)
ce.items.push(data[oldIndex]);
}
data.length = newLength;
}
if (ce)
dispatchEvent(ce);
}
//----------------------------------
// pageSize
//----------------------------------
private var _pageSize:int = 10;
public function get pageSize():int
{
return _pageSize;
}
/**
* @private
*/
public function set pageSize(value:int):void
{
_pageSize = value;
}
/**
* Resets the entire list to its initial state. All local and pending items are
* cleared.
*/
public function clearItems():void
{
var index:int = 0;
for each (var item:Object in data)
data[index++] = undefined;
if (hasEventListener(CollectionEvent.COLLECTION_CHANGE))
{
var ce:CollectionEvent = new CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
ce.kind = CollectionEventKind.RESET;
dispatchEvent(ce);
}
}
/**
* @private
*/
private static function createUpdatePCE(itemIndex:Object, oldValue:Object, newValue:Object):PropertyChangeEvent
{
const pce:PropertyChangeEvent = new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE);
pce.kind = PropertyChangeEventKind.UPDATE;
pce.property = itemIndex;
pce.oldValue = oldValue;
pce.newValue = newValue;
return pce;
}
/**
* @private
*/
private static function createCE(kind:String, location:int, item:Object):CollectionEvent
{
const ce:CollectionEvent = new CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
ce.kind = kind;
ce.location = location;
if (item is Array)
ce.items = item as Array;
else
ce.items.push(item);
return ce;
}
public function storeItemsAt(items:Vector.
= getData(start,count);
lazyList .storeItemsAt(v, start as int);
}
private function getData(start:uint, count:uint):Vector.
以上实现了一个滚动条分页的原型,但实际的项目应用中还需要改进, 比如数据的加载在滚动条抬起时请求以降低请求次数等