Class StickyHeaderContainer

java.lang.Object
com.codename1.ui.Component
com.codename1.ui.Container
com.codename1.components.StickyHeaderContainer
All Implemented Interfaces:
Animation, Editable, StyleListener, Iterable<Component>

public class StickyHeaderContainer extends Container

A scrollable container that pins the most recently scrolled-past section header to the top of its viewport, in the style of the iOS contacts list or sectioned material lists. As the next section's header rises into the pinned slot the previous header is replaced through a configurable scroll-driven transition: a directional slide where the rising header pushes the pinned one up and out, an instant cover where the rising header simply slides over the pinned one, or a fade where the pinned header fades to transparency as the next section closes the gap. Transitions are driven by scroll position so the visual stays in sync with the user's gesture and there is no time-based animation that lags behind a slow drag or skips ahead on a fling.

Sections are added with addSection(header, content). The header is a real component that participates in the scroll: when it is the active section's header it is moved into a pinned overlay slot at the top of the container, and a same-sized invisible placeholder is left behind in the scroll content so nothing jumps. Because the pinned header is the same instance, action listeners and child components remain interactive while it is pinned.

StickyHeaderContainer sticky = new StickyHeaderContainer();
sticky.setTransitionStyle(StickyHeaderContainer.TRANSITION_SLIDE);
for (char c = 'A'; c <= 'Z'; c++) {
    Label header = new Label("" + c, "StickyHeader");
    Container items = new Container(BoxLayout.y());
    for (int i = 0; i < 5; i++) {
        items.add(new Label(c + " entry " + i));
    }
    sticky.addSection(header, items);
}
form.add(BorderLayout.CENTER, sticky);
  • Field Details

    • TRANSITION_NONE

      public static final int TRANSITION_NONE
      Replace the pinned header without a fade or shift. As the next section's header rises into the pinned slot from below it slides over the pinned header (which is hidden during the overlap) and then takes its place once it reaches the top.
      See Also:
    • TRANSITION_SLIDE

      public static final int TRANSITION_SLIDE
      As the next section's header rises into the pinned slot from below it pushes the pinned header up and out of the slot in sync with the scroll, replacing it once the rising header reaches the top.
      See Also:
    • TRANSITION_FADE

      public static final int TRANSITION_FADE
      As the next section's header rises into the pinned slot from below the pinned header fades to transparency, revealing the rising header behind it. The swap happens once the rising header reaches the top and the pinned header has fully faded.
      See Also:
  • Constructor Details

    • StickyHeaderContainer

      public StickyHeaderContainer()
      Creates an empty sticky header container. Add sections via addSection(header, content).
  • Method Details

    • addSection

      public StickyHeaderContainer addSection(Component header, Component content)
      Adds a section consisting of a sticky header and its content. The content may be null for a header-only section. Returns this for chaining.
    • addSection

      public StickyHeaderContainer addSection(Component header)
      Adds a header-only section.
    • getScrollContainer

      public Container getScrollContainer()
      Returns the inner scrolling container that hosts the section content. Use this to add non-section components such as a footer, or for programmatic scrolling via setScrollPosition(int).
    • getStickyHost

      public Container getStickyHost()
      Returns the overlay container that hosts the currently-pinned header. While a section is active its header lives here; otherwise the host is empty and zero-height.
    • getStickyHeaders

      public List<Component> getStickyHeaders()
      Returns an unmodifiable view of the registered sticky headers in the order they were added.
    • getActiveSectionIndex

      public int getActiveSectionIndex()
      Returns the index of the currently pinned section, or -1 if no header is currently pinned.
    • setTransitionStyle

      public void setTransitionStyle(int style)
      Selects how the pinned header is replaced when the next section rises into the slot. One of TRANSITION_NONE, TRANSITION_SLIDE (default) or TRANSITION_FADE.
    • getTransitionStyle

      public int getTransitionStyle()
    • setTransitionDurationMillis

      public void setTransitionDurationMillis(int millis)
      Retained for API compatibility. Transitions are now scroll-driven so the per-frame duration no longer affects visuals; the value is validated and stored but otherwise unused.
    • getTransitionDurationMillis

      public int getTransitionDurationMillis()
    • isTransitionInProgress

      public boolean isTransitionInProgress()
      Returns true while the next section's header is overlapping the pinned slot, i.e. the scroll-driven transition is mid-flight.
    • getTransitionProgress

      public float getTransitionProgress()
      Returns the progress of the in-flight transition as a fraction in [0, 1]: 0 when the next section is just touching the slot from below and 1 when it has fully displaced the pinned header. Returns 0 when no transition is in progress.
    • setScrollPosition

      public void setScrollPosition(int y)
      Sets the scroll position of the inner scroll container. The value is clamped to the valid range and triggers a sticky-header recompute.
    • getScrollPosition

      public int getScrollPosition()
      Returns the current scroll position of the inner scroll container.
    • clearSections

      public void clearSections()
      Removes all sections and content from the container.
    • updateSticky

      public void updateSticky()
      Recomputes which section header should be pinned and how far the next section has displaced it. Called internally on every scroll event; call it explicitly when section content has been mutated outside of a normal layout cycle.