Class SwipeDeck

All Implemented Interfaces:
Animation, Editable, StyleListener, Iterable<Component>

public class SwipeDeck extends Container

A stack of cards the user swipes away one at a time, left or right (and optionally up), as in a dating or flash card app.

SwipeDeck deck = new SwipeDeck();
deck.addCard(new Label("First"));
deck.addCard(new Label("Second"));
deck.addCard(new Label("Third"));
deck.setSwipeListener((card, direction) ->
        Log.p(direction == SwipeDeck.RIGHT ? "Liked" : "Passed"));

The top card follows the finger and tilts with the drag. Released past a threshold, a third of the deck's width by default, it flies off that side and the listener is told the direction; released short of it, it springs back. The next two cards show behind the top one, a little smaller. #swipeLeft(), #swipeRight() and #swipeUp() dismiss the top card from code, and #undo() brings back the last dismissed card. The deck takes the pointer events over its area, so a card shows but its own buttons do not respond to taps.

  • Field Details

  • Constructor Details

    • SwipeDeck

      public SwipeDeck()
      An empty deck.
  • Method Details

    • addCard

      public void addCard(Component card)

      Adds a card to the bottom of the deck.

      Parameters
      • card: the card
    • clearCards

      public void clearCards()
      Removes every card and forgets the undo history.
    • getCardCount

      public int getCardCount()

      The number of cards still in the deck.

      Returns

      the count, including the top card

    • getTopCard

      public Component getTopCard()

      The card on top.

      Returns

      the top card, or null when the deck is empty

    • setSwipeListener

      public void setSwipeListener(SwipeDeck.SwipeListener listener)

      Sets the listener told when a card leaves.

      Parameters
      • listener: the listener, or null
    • setUpEnabled

      public void setUpEnabled(boolean upEnabled)

      Lets the user swipe a card up as well as sideways.

      Parameters
      • upEnabled: true to accept an upward swipe
    • isUpEnabled

      public boolean isUpEnabled()

      Whether an upward swipe dismisses a card.

      Returns

      true when enabled

    • setThreshold

      public void setThreshold(float threshold)

      Sets how far a card must travel to be dismissed.

      Parameters
      • threshold: a fraction of the deck's width (or height for up), 0.33 by default
    • swipeLeft

      public void swipeLeft()
      Dismisses the top card to the left.
    • swipeRight

      public void swipeRight()
      Dismisses the top card to the right.
    • swipeUp

      public void swipeUp()
      Dismisses the top card over the top.
    • undo

      public boolean undo()

      Brings back the last dismissed card to the top of the deck.

      Returns

      true if there was a card to bring back

    • isAnimating

      public boolean isAnimating()

      Whether a card is flying off or springing back.

      Returns

      true while animating

    • directionFor

      public int directionFor(int dx, int dy)

      The direction a release at an offset decides, before any animation.

      Parameters
      • dx: horizontal travel of the top card

      • dy: vertical travel, negative upward

      Returns

      #LEFT, #RIGHT, #UP, or 0 for springing back

    • initComponent

      protected void initComponent()
      Description copied from class: Component
      Allows subclasses to bind functionality that relies on fully initialized and "ready for action" component state
      Overrides:
      initComponent in class Component
    • deinitialize

      protected void deinitialize()
      Description copied from class: Component
      Invoked to indicate that the component initialization is being reversed since the component was detached from the container hierarchy. This allows the component to deregister animators and cleanup after itself. This method is the opposite of the initComponent() method.
      Overrides:
      deinitialize in class Component
    • getComponentAt

      public Component getComponentAt(int x, int y)
      Description copied from class: Container

      Returns a Component at coordinate (x, y).

      WARNING: This method may return components that are disabled, or invisible, or that do not respond to pointer events. If you are looking for the top-most component that responds to pointer events, you should use int) as it is guaranteed to return a component with Component#respondsToPointerEvents() true; or null if none is found at the coordinate.

      Parameters
      • x: absolute screen location

      • y: absolute screen location

      Returns

      a Component if found, null otherwise

      See also
      • Component#contains

      • #getResponderAt(int, int)

      Overrides:
      getComponentAt in class Container
    • isStickyDrag

      protected boolean isStickyDrag()
      Description copied from class: Component

      Returns true if the component is interested in receiving drag/pointer release events even after the gesture exceeded its boundaries. This is useful for spinners etc. where the motion might continue beyond the size of the component

      Returns

      false by default

      Overrides:
      isStickyDrag in class Component
    • pointerPressed

      public void pointerPressed(int x, int y)
      Description copied from class: Container

      If this Component is focused, the pointer pressed event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerPressed in class Container
    • pointerDragged

      public void pointerDragged(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer dragged event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerDragged in class Component
    • pointerReleased

      public void pointerReleased(int x, int y)
      Description copied from class: Component

      If this Component is focused, the pointer released event will call this method

      Parameters
      • x: the pointer x coordinate

      • y: the pointer y coordinate

      Overrides:
      pointerReleased in class Component
    • paint

      public void paint(Graphics g)
      Description copied from class: Container

      This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API's to let the PLAF perform the rendering.

      Parameters
      • g: the component graphics
      Specified by:
      paint in interface Animation
      Overrides:
      paint in class Container