Class FlowManager<T,C extends Cell<T>>
- Type Parameters:
T- the type of itemsC- the type of cell used
FlowManager is responsible for managing the flow's viewport, the cells.
It stores the current state of the viewport at any time with two properties:
- The state property which holds an object of type FlowState
- The last range property which holds the last range of displayed items, as a IntegerRange
-
Property Summary
PropertiesTypePropertyDescriptionReadOnlyObjectProperty<io.github.palexdev.mfxcore.base.beans.range.NumberRange<Integer>>Specifies the current range of displayed items as aIntegerRangeobject.Specifies the current state of the viewport as aFlowStateobject. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears the viewport.io.github.palexdev.mfxcore.base.beans.range.NumberRange<Integer>Gets the value of thelastRangeproperty.getState()Gets the value of thestateproperty.protected VirtualFlow<T,C> protected TindexToItem(int index) Converts the given index to an item from the list.voidinit()This method is responsible for filling the viewport with the necessary number of cells.final booleanfinal intitemsNum()protected CitemToCell(T item) Converts the given item to a cell usingVirtualFlow.cellFactoryProperty().ReadOnlyObjectProperty<io.github.palexdev.mfxcore.base.beans.range.NumberRange<Integer>>Specifies the current range of displayed items as aIntegerRangeobject.voidonListChange(ListChangeListener.Change<? extends T> c) This is responsible for updating the viewport state whenever a change occurs in the items list.voidonScroll()This is responsible for updating the viewport state on scroll.voidreset()protected voidsetLastRange(io.github.palexdev.mfxcore.base.beans.range.IntegerRange lastRange) Sets the value of thelastRangeproperty.protected voidSets the value of thestateproperty.Specifies the current state of the viewport as aFlowStateobject.
-
Property Details
-
state
Specifies the current state of the viewport as aFlowStateobject.- See Also:
-
lastRange
public ReadOnlyObjectProperty<io.github.palexdev.mfxcore.base.beans.range.NumberRange<Integer>> lastRangePropertySpecifies the current range of displayed items as aIntegerRangeobject.
-
-
Method Details
-
init
public void init()This method is responsible for filling the viewport with the necessary number of cells. So, it is a bit more than just an initializing method, since this is also called for example when the viewport size changes and cells need to be added or removed. For this reason this method works in a general way:First we compute the range of items we want to display by using the
OrientationHelper. Specifically we get:the number of needed cells with
OrientationHelper.maxCells()the end index with
Math.max(start + num - 1, itemsNum - 1)the start index with
The next step is to instantiate a newOrientationHelper.firstVisible(). It may happen that the difference between the end and start indexes doesn't equal to the number of needed cells, for this reason we prevent any error from occurring by "correcting" the start index as follows:Math.max(end - num + 1, 0)FlowStatewhich will be the new state of the viewport, and the current "old" state.If the old state is
FlowState.EMPTYthen from start to end index new cells are created and added to the new state. Then both the state property and last range property are updated and finallyVirtualFlow.requestViewportLayout()is called.In any other case it means we are in a situation where cells need to either be supplied or removed.
From start to end index we check if the old state contains the i-th cell, if that's the case the cell is removed from the old state and added to the new one. Otherwise, a new cell is created and added to the new state.
So, how the supply/remove works:
The new state carries all the needed cells, a listener in the
VirtualFlowSkinwill then update the viewport children if needed. All the cells which remain in the old state are the one to be removed, but since only the new state determines the viewport's content, the only operation left is to dispose these unneeded ones withCell.dispose().Finally, both the state property and last range property are updated and
VirtualFlow.requestViewportLayout()is called. -
onScroll
public void onScroll()This is responsible for updating the viewport state on scroll. If the current state isFlowState.EMPTYor the items list is empty, exits immediately.The first step is to compute the new range of items to display. Then the new state is updated only if the current range and the computed one are different, the new state is built with
FlowState.transition(IntegerRange).The second step is to call
VirtualFlow.requestViewportLayout(), but this happens only if the last range and the new range are not equal. -
onListChange
This is responsible for updating the viewport state whenever a change occurs in the items list. There are three separate situations:1) The items list is empty, calls
clear()and exits2) The current state is
FlowState.EMPTY, callsinit()and exits3) The given change is processed by using the
ListChangeHelperutility class, then the new state is computed by usingFlowState.transition(List), finallyVirtualFlow.requestViewportLayout()is called and the last range property is updated. -
clear
public void clear()Clears the viewport. Sets the state toFlowState.EMPTY. -
reset
public void reset() -
indexToItem
Converts the given index to an item from the list. -
itemToCell
Converts the given item to a cell usingVirtualFlow.cellFactoryProperty(). -
itemsNum
public final int itemsNum()- Returns:
- the number of items in the list
-
itemsEmpty
public final boolean itemsEmpty()- Returns:
- whether the list is empty
-
getVirtualFlow
- Returns:
- the virtual flow instance associated to this manager
-
getState
Gets the value of thestateproperty.- Property description:
- Specifies the current state of the viewport as a
FlowStateobject. - Returns:
- the value of the
stateproperty - See Also:
-
stateProperty
Specifies the current state of the viewport as aFlowStateobject.- Returns:
- the
stateproperty - See Also:
-
setState
Sets the value of thestateproperty.- Property description:
- Specifies the current state of the viewport as a
FlowStateobject. - Parameters:
state- the value for thestateproperty- See Also:
-
getLastRange
Gets the value of thelastRangeproperty.- Property description:
- Specifies the current range of displayed items as a
IntegerRangeobject. - Returns:
- the value of the
lastRangeproperty - See Also:
-
lastRangeProperty
public ReadOnlyObjectProperty<io.github.palexdev.mfxcore.base.beans.range.NumberRange<Integer>> lastRangeProperty()Specifies the current range of displayed items as aIntegerRangeobject.- Returns:
- the
lastRangeproperty - See Also:
-
setLastRange
protected void setLastRange(io.github.palexdev.mfxcore.base.beans.range.IntegerRange lastRange) Sets the value of thelastRangeproperty.- Property description:
- Specifies the current range of displayed items as a
IntegerRangeobject. - Parameters:
lastRange- the value for thelastRangeproperty- See Also:
-