CollectionView
A scrollable collection view that efficiently displays large data sets using view recycling.
CollectionView is designed for performance when displaying many items by only creating views for visible items and recycling them as the user scrolls. It supports both vertical and horizontal scrolling layouts.
Key features:
- Efficient view recycling for large data sets
- Customizable layouts via CollectionViewLayout
- Automatic item visibility management
- Smooth scrolling to specific items
- Multiple item behavior modes (RECYCLE, FREEZE, LAZY)
var collection = new CollectionView();
collection.size(400, 600);
// Set up a flow layout
var layout = new CollectionViewFlowLayout();
layout.itemSize = { width: 100, height: 100 };
layout.spacing = 10;
collection.collectionViewLayout = layout;
// Implement data source
collection.dataSource = new MyCustomCollectionViewDataSource();
Instance Members
Reference to the current layout if it's a CollectionViewFlowLayout. This provides optimized access to flow layout specific properties. Will be null if using a different layout type.
The layout object responsible for positioning items in the collection. Changing the layout will trigger a full relayout of all items.
The data source that provides items for the collection view. Setting a new data source will reload all data and recreate visible items.
The data source must implement:
- collectionViewSize(): Number of items
- collectionViewItemAtIndex(): Create/configure item views
- collectionViewItemFrameAtIndex(): Set item dimensions
- collectionViewReleaseItemAtIndex(): Handle item recycling
Whether to automatically destroy item views when they're removed. Set to false if you want to manage item lifecycle manually. Default is true for automatic memory management.
Maximum number of recycled views to keep in memory. Higher values can improve scrolling performance but use more memory. Lower values save memory but may cause more view creation during scrolling. Default is 1.
Controls how item views are assigned depth values for rendering order.
- INCREMENT: Each item has higher depth (later items on top)
- DECREMENT: Each item has lower depth (earlier items on top)
- SAME: All items at same depth (order by index)
- CUSTOM: Manual depth assignment Default is SAME.
Read-only array of frames representing the position and size of each item. Frames are computed by the layout and used for visibility culling.
Determines how item views are managed:
- RECYCLE: Reuse views for performance (default)
- FREEZE: Keep all created views active
- LAZY: Create views only when visible
RECYCLE is recommended for large data sets.
Reloads all data from the data source. This will:
- Destroy all existing item views
- Query the data source for the new item count
- Create frames for all items
- Trigger a layout update
Call this when your underlying data changes.
Finds the index of the item closest to the given coordinates.
Name | Type | Default | Description |
---|---|---|---|
x |
Float | X coordinate to test | |
y |
Float | Y coordinate to test | |
includeScroll |
Bool | true |
Whether to account for current scroll position |
Returns | Description |
---|---|
Int | Index of the closest item, or -1 if no items |
Updates which items are visible and manages view recycling. This is called automatically when scrolling or layout changes.
The method will:
- Determine which frames are in the visible area
- Recycle views that moved off-screen
- Create or reuse views for newly visible items
- Position and size all visible views
getTargetScrollXForItem(itemIndex: Int, ?itemPosition: CollectionViewItemPosition = CollectionViewItemPosition.ENSURE_VISIBLE): Float
Calculates the scroll X position needed to show an item at the desired position.
Name | Type | Default | Description |
---|---|---|---|
itemIndex |
Int | Index of the item to scroll to | |
itemPosition |
CollectionViewItemPosition | CollectionViewItemPosition.ENSURE_VISIBLE |
Where to position the item (START, MIDDLE, END, ENSURE_VISIBLE) |
Returns | Description |
---|---|
Float | Target scroll X value |
getTargetScrollYForItem(itemIndex: Int, ?itemPosition: CollectionViewItemPosition = CollectionViewItemPosition.ENSURE_VISIBLE): Float
Calculates the scroll Y position needed to show an item at the desired position.
Name | Type | Default | Description |
---|---|---|---|
itemIndex |
Int | Index of the item to scroll to | |
itemPosition |
CollectionViewItemPosition | CollectionViewItemPosition.ENSURE_VISIBLE |
Where to position the item (START, MIDDLE, END, ENSURE_VISIBLE) |
Returns | Description |
---|---|
Float | Target scroll Y value |
scrollToItem(itemIndex: Int, ?itemPosition: CollectionViewItemPosition = CollectionViewItemPosition.ENSURE_VISIBLE): Void
Immediately scrolls to show the specified item.
Name | Type | Default | Description |
---|---|---|---|
itemIndex |
Int | Index of the item to scroll to | |
itemPosition |
CollectionViewItemPosition | CollectionViewItemPosition.ENSURE_VISIBLE |
Where to position the item: - ENSURE_VISIBLE: Scroll minimum amount to make visible - START: Position at start of view - MIDDLE: Center in view - END: Position at end of view |
smoothScrollToItem(itemIndex: Int, ?itemPosition: CollectionViewItemPosition = CollectionViewItemPosition.ENSURE_VISIBLE, ?duration: Float = 0.15, ?easing: Anonymous): Void
Smoothly animates scrolling to show the specified item.
Name | Type | Default | Description |
---|---|---|---|
itemIndex |
Int | Index of the item to scroll to | |
itemPosition |
CollectionViewItemPosition | CollectionViewItemPosition.ENSURE_VISIBLE |
Where to position the item (see scrollToItem) |
duration |
Float | 0.15 |
Animation duration in seconds (default: 0.15) |
easing |
Anonymous | (optional) | Easing function for the animation |
Creates a new CollectionView with a default flow layout.
Private Members
Pool of recycled views available for reuse. Managed automatically based on itemsBehavior and maxReusableViewsCount.
Previous layout width, used to detect size changes.
Previous layout height, used to detect size changes.
Metadata
Name | Parameters |
---|---|
:build |
tracker.macros.EventsMacro.build() |
:autoBuild |
tracker.macros.EventsMacro.build() |
:build |
ceramic.macros.EntityMacro.buildForCompletion() |
:autoBuild |
ceramic.macros.EntityMacro.buildForCompletion() |