Skip to content

Commit

Permalink
Remove scroll event handler upon removal from scenegraph
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanMartinez committed Apr 10, 2018
1 parent 7eb0a6b commit ff7ada9
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/main/java/org/fxmisc/flowless/CellListManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.function.Function;

import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.input.ScrollEvent;

Expand All @@ -24,6 +25,7 @@ final class CellListManager<T, C extends Cell<T, ? extends Node>> {
private final MemoizationList<C> cells;
private final LiveList<C> presentCells;
private final LiveList<Node> cellNodes;
private final EventHandler<ScrollEvent> pushToOwner = this::pushScrollEvent;

private final Subscription presentCellsSubscription;

Expand Down Expand Up @@ -94,21 +96,26 @@ private C cellForItem(T item) {
EventStreams.nonNullValuesOf(node.sceneProperty())
.subscribeForOne(scene -> {
node.applyCss();

node.addEventHandler(ScrollEvent.ANY, pushToOwner);
EventStreams.valuesOf(node.sceneProperty())
.filter(scene0 -> scene0 == null)
.subscribeForOne(nullValue -> node.removeEventHandler(ScrollEvent.ANY, pushToOwner));
});

// Make cell initially invisible.
// It will be made visible when it is positioned.
node.setVisible(false);

if (cell.isReusable()) {
// if cell is reused i think adding event handler
// would cause resource leakage.
node.setOnScroll(this::pushScrollEvent);
node.setOnScrollStarted(this::pushScrollEvent);
node.setOnScrollFinished(this::pushScrollEvent);
} else {
node.addEventHandler(ScrollEvent.ANY, this::pushScrollEvent);
}
// if (cell.isReusable()) {
// // if cell is reused i think adding event handler
// // would cause resource leakage.
// node.setOnScroll(this::pushScrollEvent);
// node.setOnScrollStarted(this::pushScrollEvent);
// node.setOnScrollFinished(this::pushScrollEvent);
// } else {
// node.addEventHandler(ScrollEvent.ANY, this::pushScrollEvent);
// }

return cell;
}
Expand Down

0 comments on commit ff7ada9

Please sign in to comment.