Reorderable lists
A playlist, a list of tasks, the slots of a toolbar: the user drags an item to a new place, and the others make room.
A ReorderableFlexNode is a flex layout whose children the user reorders.
You will learn
- How to make a list that the user reorders
- How to react when the order changes
- How to drag the items by a handle only
- How to pin items in place
Let the user reorder a list
Section titled “Let the user reorder a list”ReorderableFlexNode.vertical(60, 40, 480).onReorderEnd(event -> { final String song = event.getChild().getLabel().getText(); this.moved.set(song + " from " + event.getPrevious() + " to " + event.getIndex());}).margin(10D).<ReorderableFlexNode>body(list -> { for (final String song : new String[] {"Intro", "Ocean", "Night drive", "Echoes", "Outro"}) { RectNode .create(0, 0, 480, 70) .color(PlaylistUI.ELEMENT) .hoveredColor(PlaylistUI.HOVER) .label(Text.create(song, info)) .self(row -> row.effect(ShadowNodeEffect.create( () -> list.isDragging(row) ? PlaylistUI.SHADOW : Color.TRANSPARENT, () -> 32F ))) .attach(list); }}).attach(card);ReorderableFlexNode has the factories and the settings of a FlexNode: vertical(x, y, width), horizontal(x, y, height), margin(...), align(...).
- A press of the left button on a child starts the drag; the child follows the pointer along the axis of the list only, drawn above the others.
- The other children slide to make room as soon as the dragged one passes half of a neighbor.
- At the release, the child eases into its slot, and the order of the children changes for good.
- Near an edge of a parent that scrolls, the drag scrolls it.
Here each row casts a shadow while the list reports it dragged, so that it stands out from the neighbor that it covers.
A child that consumes the press, such as a button or a text field, keeps the drag from starting there.
ReorderableFlexNode.autoDrag (false) turns the drag on press off for the whole list.
React to a new order
Section titled “React to a new order”The playlist of the first example writes the move once the drag ends:
.onReorderEnd(event -> { final String song = event.getChild().getLabel().getText(); this.moved.set(song + " from " + event.getPrevious() + " to " + event.getIndex());})| Event | When it runs |
|---|---|
ReorderableFlexNode.onReorderStart | A child starts to be dragged |
ReorderableFlexNode.beforeReorder | The dragged child is about to take a new slot; cancel() keeps it in its slot |
ReorderableFlexNode.onReorder | The dragged child took a new slot, during the drag |
ReorderableFlexNode.onReorderEnd | The drag ended, and the order is final |
Each listener receives a NodeReorderEvent: getChild() is the dragged child, getPrevious() is its index before and getIndex() its index after, counted from 0.
Save the new order in onReorderEnd: it runs once per drag, also when the list or the child is removed during the drag.
The dragged child runs its own drag events, onDragStart to onDragDrop, and its beforeDragStart can refuse the drag.
During a drag, ReorderableFlexNode.getLogicalOrder gives the order that the children take at the release, ReorderableFlexNode.isDragging tells which child is dragged, and ReorderableFlexNode.getChildIndex gives the slot of a child.
Drag by a handle only
Section titled “Drag by a handle only”With autoDrag(false), a handle starts the drag with ReorderableFlexNode.startDrag:
final ReorderableFlexNode list = ReorderableFlexNode.vertical(60, 40, 480).autoDrag(false).margin(10D).attach(card);final RectNode header = RectNode.create(0, 0, 480, 70).color(HandleListUI.ACCENT).label(Text.create("To do", dark)).attach(list);
for (final String task : new String[] {"Write", "Review", "Ship"}) { RectNode .create(0, 0, 480, 70) .color(HandleListUI.ELEMENT) .label(Text.create(task, info)) .body(item -> { RectNode .create(420, 15, 40, 40) .color(HandleListUI.ACCENT) .cursor(Cursor.MOVE) .handleMousePressed(event -> { if (event.getNode().isHovered() && event.getButton().isLeft()) { list.startDrag(item); event.consume(); } }) .attach(item); }) .attach(list);}list.pin(header);The handle receives every press of the UI, so it tests isHovered() before it starts the drag (Hear the raw input).
ReorderableFlexNode.endDrag ends a drag from your code.
Pin items in place
Section titled “Pin items in place”The header of HandleListUI stays first, whatever the user drags:
list.pin(header);ReorderableFlexNode.pin keeps children in their slot: the user cannot drag them, and the other children move around them.
ReorderableFlexNode.unpin frees them again, and ReorderableFlexNode.isPinned tells whether a child is pinned.
- A
ReorderableFlexNodeis a row or a column whose children the user drags to a new slot, with the settings of aFlexNode. - The order changes for good at the release;
onReorderEndgives the old and the new index, once per drag. - A child that consumes the press cannot be dragged by it;
autoDrag(false)withstartDragdrags from a handle. pinkeeps children in their slot.
Try it
Section titled “Try it”A horizontal toolbar.
Turn PlaylistUI into a row of five square slots of 80 by 80.
Solution
The horizontal factory takes the height of the row:
ReorderableFlexNode.horizontal(60, 40, 80).margin(10D)Each child is a RectNode of 80 by 80.
Keep the last one.
In PlaylistUI, keep “Outro” at the end of the list.
Solution
Pin it once the children exist:
list.pin(list.getChildren().get(list.getChildren().size() - 1));