[英]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.