The ListBox Component that comes with Flash can be handy, but it has some shortcomings that limit its usefulness. For example, the scrolling logic causes items to reload each time they come into view. If you are loading external content, this causes a bothersome blinking effect. Also the CellRenderer API is a little difficult to work with for some folks. So, I put together some ActionsScript 2 classes that use the scrollpane component to build a scrolling list of items. It works similar to a ListBox component but with some additional functionality and without all these problems.
You can download the source from here.
Note that in the example fla, not all the items in the list have a caption under them. This is not a bug, just an example to show that the captions were made optional.
It looks like a lot of code, but for general usage the only classes you need to pay much attention to are:
ScrollList - the main class that you instantiate
IListItem - the interface that you implement in order to create an item template.
Other important classes...
AssetListItem - is just a simple example implementation of IListItem so you can easily see how you might implement some of the methods. In fact you might use the exact implementation for many of the methods. The methods that will likely be specific to your implementation are:
setData - here is where you process the xml node that is passed into the item and use it to configure the display of the item.
isReady - This is used in case the item loads external content. It should return false until the content is loaded. If there is no external content, it should just return true.
showSelectedState / showNormalState - these you might leave as they are but it depends on how you want to handle the display of the corresponding states of the items.
destroy - this one applies if you plan on needing to dynamically remove items from a list. The example just deletes the movieclip, but if you are concerned about memory you might want to make sure everything is cleaned up before deleting the clip.
ListBase - you might also look at this class since ScrollList inherits from it. There are a number of useful public methods in there.
Some Features...
- It allows any scroll pane implementation to be used. You just have to write a simple adapter class. The zip file includes adapters for macromedia's scrollpane and MCOM's scroll pane.
- The scroll wheel will work where supported.
- Supports the ability to drag and drop items within the list to change the order. All you have to do is have your item implementation dispatch "itemDrag" and "itemDrop" events at the proper time, setting itself as the event target. The ListBase class takes care of the rest for you.
- Supports adding and removing items on the fly.
- Dispatches some events: itemSelected, itemAdded, layoutComplete. It would be easy to add other events if desired.
- If you want to have items that dynamically change size ( for example the user clicks to expand an item for more detail or something like that), this is supported. You simply make your item dispatch a "itemChangeSize" event at the appropriate time. I've even had items expand into a nested lists. The nested list was a non scrolling list built on the ListBase class.
- Supports most methods you see in collection API's.
I'm sorry that I don't have better documentation for this, but this is not an official open source release, just me responding to requests to see the code. If there are any questions about how to use it or feedback on the code please let me know. Perhaps someday I can make an official release. I hope to soon post some examples of the functionality.
If anyone uses this I would appreciate an email describing your project or a comment on this blog post with a link to your project.