我想在tableview中收到有关滚动事件的通知。
我当前有这个:
scrollBar = (ScrollBar) tableView.lookup(".scroll-bar:vertical");
scrollBar.valueProperty().addListener((observable, oldValue, newValue) -> {
System.err.println(newValue);
if ((newValue != null) && ((Double) newValue == 0.0)) {
addMoreDataTop(4);
}
if ((newValue != null) && ((Double) newValue == 1.0)) {
addMoreDataBottom(4);
}
});
只要新值在0到1.0之间(只要值正在变化)。
。 。但我特别是对scrollevents "在列表的边缘"上感兴趣,这意味着当滚动值已经为0或1.0 。
。valueproperty不再改变,因此未通知听众。但是滚动(意图)无论如何正在发生。我可以通知有关此类滚动事件的通知,这些事件不会更改列表/表的滚动?
基于Slaw的评论,我想到了:
tableView.setOnScroll(e -> {
if (e.getDeltaY() > 0) {
fillAtTop();
} else if (e.getDeltaY() < 0) {
fillAtBottom();
}
});
这是按预期工作的,因为Tableview的滚动条将消耗更改表的"滚动状态"的滚动事件。
只有在滚动栏不更改时,才会调用注册的侦听器。
。当用户滚动到表的开头或结尾时,我用它来懒洋洋地加载表内容。
这是提取的MWE:
public class MainApp extends Application {
final AtomicInteger cnt = new AtomicInteger(0);
final ObservableList<String> content = FXCollections.observableArrayList();
public static void main(final String[] args) {
launch(args);
}
@Override
public void start(final Stage primaryStage) throws Exception {
preFillContent();
final TableView<String> tableView = new TableView<>();
final TableColumn<String, String> c1 = new TableColumn<>("C1");
c1.setCellValueFactory(e -> new ReadOnlyObjectWrapper<>(e.getValue().toString()));
tableView.getColumns().add(c1);
tableView.setItems(content);
tableView.setOnScroll(e -> {
if (e.getDeltaY() > 0) {
fillAtTop();
} else if (e.getDeltaY() < 0) {
fillAtBottom();
}
});
final StackPane root = new StackPane();
root.getChildren().add(tableView);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
private void preFillContent() {
for (int i = 0; i < 10; i++) {
content.add("element " + cnt.incrementAndGet());
}
}
private void fillAtBottom() {
content.add("element " + cnt.incrementAndGet());
}
private void fillAtTop() {
content.add(0, "element " + cnt.incrementAndGet());
}
}