How to use the PopUpListView component

The PopUpListView class displays a button, that when triggered, renders the items from a data collection in a pop-up list view.

Live preview of the PopUpListView component

The Basics

Start by creating a PopUpListView control, and add it to the display list.

var popUpListView = new PopUpListView();
addChild(popUpListView);

Data provider

To render some data in the list view, pass in a collection that contains an object for each row.

popUpListView.dataProvider = new ArrayCollection([
    { text: "A" },
    { text: "B" },
    { text: "C" }
]);

Set the itemToText() method to get the text from each item to display in an item renderer.

popUpListView.itemToText = function(item:Dynamic):String {
    return item.text;
};

Items in the collection are not required to be anonymous structures, like in the example above. Class instances are allowed too (and encouraged as a best practice; you should prefer classes over anonymous structures).

Selection

Add an event listener for Event.CHANGE to perform an action when the user selects a different item.

popUpListView.addEventListener(Event.CHANGE, popUpListView_changeHandler);

Check for the new value of the selectedItem property in the listener.

function popUpListView_changeHandler(event:Event):Void {
    var popUpListView = cast(event.currentTarget, PopUpListView);
    trace("PopUpListView selectedItem change: " + popUpListView.selectedItem.text);
}

Alternatively, the value of the selectedIndex property references the index of the items in the list view's collection, in the order that they were added.

function popUpListView_changeHandler(event:Event):Void {
    var popUpListView = cast(event.currentTarget, PopUpListView);
    trace("PopUpListView selectedIndex change: " + popUpListView.selectedIndex);
}

Add or remove items

To add a new item at the end, pass an object to the data provider's add() method.

var newItem = { text: "New Item" };
popUpListView.dataProvider.add(newItem);

To add a new item at a specific position, pass an object to the data provider's addAt() method.

var newItem = { text: "First Item" };
popUpListView.dataProvider.addAt(newItem, 0);

In the example above, a new item is added to the beginning.

Similarly, to remove an item, call remove() or removeAt() on the collection.

popUpListView.dataProvider.removeAt(0);

Item renderers

An item renderer is a Feathers UI component that displays a single item from a data collection inside a component like ListView or PopUpListView. In other words, the ListView displayed by a PopUpListView typically contains multiple item renderers — with each one rendering a different item from the collection.

Feathers UI provides a default ItemRenderer class, which can display data in many different ways that cover a variety of common use-cases. However, components like PopUpListView also support custom item renderers, which allow developers to render the list view's data in infinite unique ways.

Consider a collection of items with the following format.

{ name: "Pizza", icon: "https://example.com/img/pizza.png" }

While the default ItemRenderer class can easily display some text and an image, creating a custom item renderer for this simple data will be a good learning exercise.

A custom item renderer designed to display this data might use a Label to display the text, and an AssetLoader to display the image. The following example creates a DisplayObjectRecycler which instantiates these components and adds them to a LayoutGroupItemRenderer — a special base class for custom item renderers.

var recycler = DisplayObjectRecycler.withFunction(() -> {
    var itemRenderer = new LayoutGroupItemRenderer();

    var layout = new HorizontalLayout();
    layout.gap = 6.0;
    layout.paddingTop = 4.0;
    layout.paddingBottom = 4.0;
    layout.paddingLeft = 6.0;
    layout.paddingRight = 6.0;
    itemRenderer.layout = layout;

    var icon = new AssetLoader();
    icon.name = "loader";
    itemRenderer.addChild(icon);

    var label = new Label();
    label.name = "label";
    itemRenderer.addChild(label);

    return itemRenderer;
});

Developers are not required to use the LayoutGroupItemRenderer class. In fact, a custom item renderer may be created from any OpenFL display object, including primitives like openfl.display.Sprite and all other Feathers UI components.

Pass the DisplayObjectRecycler to the itemRendererRecycler property.

popUpListView.itemRendererRecycler = recycler;

So far, the DisplayObjectRecycler creates the item renderer, but it doesn't understand how to interpret the data yet. A custom update() method on the recycler can do that.

recycler.update = (itemRenderer:LayoutGroupItemRenderer, state:ListViewItemState) -> {
    var label = cast(itemRenderer.getChildByName("label"), Label);
    var loader = cast(itemRenderer.getChildByName("loader"), AssetLoader);

    label.text = state.text;
    loader.source = state.data.icon;
};

When the update() method is called, it receives the item renderer and an ListViewItemState object. ListViewItemState has a number of useful properties.

In this case, the value of text is displayed by the Label, and the icon field from data (remember the example item from above, with name and icon fields) is displayed by the AssetLoader. Obviously, we'll need an itemToText() function to populate the text value from the name field.

popUpListView.itemToText = function(item:Dynamic):String {
    return item.name;
};

It's always a good practice to provide a reset() method to the DisplayObjectRecycler, which will clean up a custom item renderer when it is no longer used by the PopUpListView.

recycler.reset = (itemRenderer:LayoutGroupItemRenderer, state:ListViewItemState) -> {
    var label = cast(itemRenderer.getChildByName("label"), Label);
    var loader = cast(itemRenderer.getChildByName("loader"), AssetLoader);
    label.text = "";
    loader.source = null;
};

Warning: A DisplayObjectRecycler without a reset() method could potentially cause memory leaks or other unexpected behavior, if the same data needs to be used again later.

Styles

A number of styles may be customized on the sub-components of a PopUpListView component, including styles on the button and the list view.

Button

The button in a PopUpListView component is of type Button. Its appearance may be customized globally in a theme, or it may be customized outside of a theme on an specific, individual PopUpListView.

See How to use the Button component for complete details about which styles are available for the button.

Style button globally

Use the PopUpListView.CHILD_VARIANT_BUTTON constant in a theme to provide a function that globally styles the buttons in all PopUpListView components.

styleProvider.setStyleFunction(
    Button,
    PopUpListView.CHILD_VARIANT_BUTTON,
    setPopUpListView_Button_Styles);

The function should use the following signature.

function setPopUpListView_Button_Styles(button:Button):Void {
    // ... set styles here
}

Style the button in a specific PopUpListView

The buttonFactory property may be used to customize the creation of an individual button.

popUpListView.buttonFactory = () -> {
    var button = new Button();
    // ... set styles here
    return button;
};

ListView

The list view in a PopUpListView component is of type ListView. Its appearance may be customized globally in a theme, or it may be customized outside of a theme on an specific, individual PopUpListView.

See How to use the ListView component for complete details about which styles are available for the list view.

Style list view globally

Use the PopUpListView.CHILD_VARIANT_LIST_VIEW constant in a theme to provide a function that globally styles the list views in all PopUpListView components.

styleProvider.setStyleFunction(
    ListView,
    PopUpListView.CHILD_VARIANT_LIST_VIEW,
    setPopUpListView_ListView_Styles);

The function should use the following signature.

function setPopUpListView_ListView_Styles(listView:ListView):Void {
    // ... set styles here
}

Style the list view in a specific PopUpListView

The listViewFactory property may be used to customize the creation of an individual list view.

popUpListView.listViewFactory = () -> {
    var listView = new ListView();
    // ... set styles here
    return listView;
};