【发布时间】:2018-12-24 23:46:31
【问题描述】:
ListView 控件有问题。我希望我的奇数行和偶数行有不同的颜色,但我想用代码而不是 FXML 来做。例如:
- 第一行 - 绿色
- 第二行 - 红色
- 第三行 - 颜色绿色
- 第四行 - 红色
现在我有这样的东西,但这改变了所有 ListView 的背景而不是单行。
rightListView.setCellFactory(param -> new ListCell<Group>() {
@Override
protected void updateItem(Group item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null || item.getName() == null) {
setText(null);
} else {
setText(item.getName());
}
for(int k=0;k<rightListView.getItems().size();k++) {
if (k%2==0)
setStyle("-fx-background-color: blue;");
else
setStyle("-fx-background-color: red;");
}
}
});
我该如何解决这个问题?
【问题讨论】: