类 CellsManager<T,C extends Cell<T>>

java.lang.Object
io.github.palexdev.virtualizedfx.flow.simple.CellsManager<T,C>
类型参数:
T - the type of object to represent
C - the type of Cell to use

public class CellsManager<T,C extends Cell<T>> extends Object
Helper class to properly keep track of the needed cells, add them to the container, update them on scroll, and update the cells layout.

The 'system' is quite simple yet super efficient: it keeps a List called cellsPool that contains a number of cells (this number is the maximum number of cells that can be shown into the viewport). During initialization the cellPool is populated by initCells(int) and then added to the container, they are also updated to be sure they are showing the right content. On scroll updateCells(int) is called, this method processes the needed updates to do, see (CellsManager<T,C extends Cell<T>>.io.github.palexdev.virtualizedfx.flow.simple.CellsManager.CellUpdate), then updates the cells, updates the layout by calling processLayout(List) and finally updates the indexes range of shown items.

The important part to understand is that rather than creating new cells everytime the flow scrolls we update the already created cells, so the scroll is very smooth, and it's also efficient in terms of memory usage. This allows the flow to handle huge amounts of items (depending on cells' complexity of course).
  • 构造器概要

    构造器
    构造器
    说明
     
  • 方法概要

    修饰符和类型
    方法
    说明
    protected C
    cellForIndex(int index)
    Exchanges an index for a Cell.
    protected C
    cellForItem(T item)
    Exchanges an item for a Cell.
    protected void
    Resets the CellsManager by clearing the container's children, clearing the cellsPool, clearing the updates list and resetting the stored indexes range to [-1, -1].
    protected Map<Integer,C>
     
    protected void
    initCells(int num)
    Populates the cellsPool, adds them to the container and then calls updateCells(int) (indexes from 0 to num) to ensure the cells are displaying the right content (should not be needed though)
    void
    Called when the items list changes, if the list has been cleared calls clear() and then exits.
    protected void
    processLayout(List<CellsManager<T,C>.io.github.palexdev.virtualizedfx.flow.simple.CellsManager.CellUpdate> updates)
    Responsible for laying out the cells from a list of CellsManager<T,C extends Cell<T>>.io.github.palexdev.virtualizedfx.flow.simple.CellsManager.CellUpdates.
    protected void
    Forces processLayout(List) with the previously computed updates.
    protected void
    Drastic reset of the VirtualFlow, scroll position is set back to 0.0, clear() is called, cells are re-initialized initCells(int).
    protected void
    supplyCells(int from, int targetSize)
    Creates new cells from the given from index, keeps creating cells until the cell pool size has reached targetSize, new cells are also added to the container.
    protected void
    updateCells(int start)
    Responsible for updating the cells in the viewport.

    从类继承的方法 java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 构造器详细资料

  • 方法详细资料

    • initCells

      protected void initCells(int num)
      Populates the cellsPool, adds them to the container and then calls updateCells(int) (indexes from 0 to num) to ensure the cells are displaying the right content (should not be needed though)
      参数:
      num - the number of cells to create
    • updateCells

      protected void updateCells(int start)
      Responsible for updating the cells in the viewport.

      If the items list is empty returns immediately.

      Computes the max number of cells to show by calling OrientationHelper.computeCellsNumber() then computes the end index by adding the start index to it (clamped between 0 and number of items).

      If the list was cleared, so start is invalid, calls initCells(int) with the previous computed max, then exits as that method will then call this with a valid start index.

      If the new indexes range (start-end) is equal to the previous stored range it's not necessary to update the cells therefore exits. This check is ignored if the items list was changed (items replaced, added or removed), int this case it's needed to update.

      The next step is a bit tricky. The cellsPool indexes go from 0 to size() - 1 of course, but the start and end parameters do not start from 0, let's say I have to show items from 10 to 20, so it's needed to use an "external" counter to get the cells from 0 to size() - 1, while the items are retrieved from the list from start to end, in this loop we create CellsManager<T,C extends Cell<T>>.io.github.palexdev.virtualizedfx.flow.simple.CellsManager.CellUpdate beans to make the code cleaner, after the loop CellsManager.CellUpdate.update() is called for each bean, then the layout is updated with processLayout(List) and finally the range of shown items is updated.
      参数:
      start - the start index from which retrieve the items from the items list
    • itemsChanged

      public void itemsChanged()
      Called when the items list changes, if the list has been cleared calls clear() and then exits.

      If the last range is invalid (meaning that the container is empty) computes the max number of cells to show with OrientationHelper.computeCellsNumber(), then calls initCells(int) with that number.

      If none of the above conditions are met then we have to make several checks:

      It's needed to check if there are too many cells in the pool or too few, depending on the case, it's needed to remove/supply some cells.

      Finally the listChanged flag is set to true to force the update and updateCells(int) is called, the start index is the last start index.
    • processLayout

      protected void processLayout(List<CellsManager<T,C>.io.github.palexdev.virtualizedfx.flow.simple.CellsManager.CellUpdate> updates)
      Responsible for laying out the cells from a list of CellsManager<T,C extends Cell<T>>.io.github.palexdev.virtualizedfx.flow.simple.CellsManager.CellUpdates.

      The positioning is absolute, it always starts from [0, 0], this ensures that there is no white space above or below any cell.

      For each update (from 0 to updates.size()) cell's position is computed by OrientationHelper.layout(Node, int, double, double).

    • requestLayout

      protected void requestLayout()
      Forces processLayout(List) with the previously computed updates.
    • reset

      protected void reset()
      Drastic reset of the VirtualFlow, scroll position is set back to 0.0, clear() is called, cells are re-initialized initCells(int).

      Typically happens when the layout bounds of the VirtualFlow changed.

    • clear

      protected void clear()
      Resets the CellsManager by clearing the container's children, clearing the cellsPool, clearing the updates list and resetting the stored indexes range to [-1, -1].
    • supplyCells

      protected void supplyCells(int from, int targetSize)
      Creates new cells from the given from index, keeps creating cells until the cell pool size has reached targetSize, new cells are also added to the container.
    • cellForIndex

      protected C cellForIndex(int index)
      Exchanges an index for a Cell.

      Gets the item at the given index in the items list then calls cellForItem(Object).

    • cellForItem

      protected C cellForItem(T item)
      Exchanges an item for a Cell.

      Simply applies the VirtualFlow's cell factory to the given item.

    • getCells

      protected Map<Integer,C> getCells()
      返回:
      a map of the currently shown cells by the item's index