-
Notifications
You must be signed in to change notification settings - Fork 129
ListView
This article applies to VisUI 1.0.0
ListView (source) allows to create advanced lists. ListView is powerful and flexible, items views are created using list adapter which allows to huge view customization. List item selection can be enabled, it allows to both single and multiple selection mode. List is scrollable, you can specify custom table header and footer. Items can be sorted using supplied comparator.
SimpleListAdapater<Model> adapter = new SimpleListAdapter<Model>(array);
ListView<Model> view = new ListView<Model>(adapter);
table.add(view.getMainTable()).grow();
Creating ListView
requires to pass instance of ListAdapter
which is responsible for creating views to array items. In this case we pass SimpleListAdapter
instance which uses toString()
to create item view. Adapters also provide methods to manipulate collection elements such as add, remove etc. Those methods should always be used when manipulating collection displayed by ListView because they will automatically update ListView
. If array was modified directly adapter.itemsChanged()
must be called before user interacts with ListView
.
If you want to get notified when user has clicked an view you can set ItemClickListener
for ListView:
listView.setItemClickListener(new ItemClickListener<Model>() {
@Override
public void clicked (Model item) {
System.out.println("Clicked: " + item.name);
}
});
Creating custom adapter will give you option to customize how ListView
displays array items. Instead of implementing ListAdapter
directly your adapter class should extend ArrayAdapter
(if you are using libGDX's Array
class) or ArrayListAdapter
(if you are using ArrayList
). If you want to implement adapter for custom collection extend AbstractListAdapter
and see ArrayAdapter
or ArrayListAdapter
for example how to do so.
In this simple adapter for Array
we create VisLabel
to display String
content.
private static class ExampleAdapter extends ArrayAdapter<String, VisTable> {
public ExampleAdapter (Array<String> array) {
super(array);
}
@Override
protected VisTable createView (String item) {
VisLabel label = new VisLabel(item);
VisTable table = new VisTable();
table.left();
table.add(label);
return table;
}
}
ArrayAdapater is a generic type, first type is item type that will be stored and the second one is the type of view.
String is immutable but your items may need updating, in that case you can additionally override updateView
:
@Override
protected void updateView (VisTable view, String item) {
//update your view here...
}
Note that you need to notify adapter that items data has changed by calling adapter.itemsDataChanged()
See README for VisUI introduction.