[英]JavaFX virtualised controls use
I have to display 5000 nodes using ListView. 我必须使用ListView显示5000个节点。 Every node contains complex controls but only some text part is different in nodes. 每个节点都包含复杂的控件,但是节点中只有一些文本部分是不同的。 How can i reuse existing nodes controls to recreate my cells while scrolling 滚动时如何重用现有节点控件以重新创建单元格
The answer of James_D points into the right direction. James_D的答案指出了正确的方向。 Normally, in JavaFX you shouldn't worry about reusing existing nodes - the JavaFX framework does exactly this, out-of-the-box. 通常,在JavaFX中,您不必担心重用现有节点-JavaFX框架即开即用地做到了这一点。 If you want to implement some custom cell rendering, you need to set a cell factory, and that usually looks like this: 如果要实现一些自定义单元格渲染,则需要设置一个单元格工厂,通常如下所示:
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!
}
}
};
}
});
Please note: this should work, but is merely illustrative - we Java Devs know that eg, we would use a StringBuilder for String concatenation, especially in such cases where the code will execute very often. 请注意:这应该可行,但仅是说明性的-我们的Java开发人员知道,例如,我们将使用StringBuilder进行字符串连接,尤其是在代码执行频繁的情况下。 If you want some complex rendering, you may build that graphic with additional nodes and set them as graphics property with setGraphic(). 如果需要复杂的渲染,则可以使用其他节点构建该图形,并使用setGraphic()将其设置为graphics属性。 This works similar to the Label control: 这类似于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.