[英]JavaFX virtualised controls use
我必须使用ListView显示5000个节点。 每个节点都包含复杂的控件,但是节点中只有一些文本部分是不同的。 滚动时如何重用现有节点控件以重新创建单元格
James_D的答案指出了正确的方向。 通常,在JavaFX中,您不必担心重用现有节点-JavaFX框架即开即用地做到了这一点。 如果要实现一些自定义单元格渲染,则需要设置一个单元格工厂,通常如下所示:
listView.setCellFactory(new Callback() {
@Override
public Object call(Object param) {
return new ListCell<String>() {
// you may declare fields for some more nodes here
// and initialize them in an anonymous constructor
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty); // Default behaviour: zebra pattern etc.
if (empty || item == null) { // Default technique: take care of empty rows!!!
this.setText(null);
} else {
this.setText("this is the content: " + item);
// ... do your custom rendering!
}
}
};
}
});
请注意:这应该可行,但仅是说明性的-我们的Java开发人员知道,例如,我们将使用StringBuilder进行字符串连接,尤其是在代码执行频繁的情况下。 如果需要复杂的渲染,则可以使用其他节点构建该图形,并使用setGraphic()将其设置为graphics属性。 这类似于Label控件:
// another illustrative cell renderer:
listView.setCellFactory(new Callback() {
@Override
public Object call(Object param) {
return new ListCell<Integer>() {
Label l = new Label("X");
@Override
protected void updateItem(Integer item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
this.setGraphic(null);
} else {
this.setGraphic(l);
l.setBackground(
new Background(
new BackgroundFill(
Color.rgb(3 * item, 2 * item, item),
CornerRadii.EMPTY,
Insets.EMPTY)));
l.setPrefHeight(item);
l.setMinHeight(item);
}
}
};
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.