1 回答

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個(gè)贊
您需要CellFactory在ListView. 然后我們可以確定該單元格是否屬于List您用來(lái)填充Listview. 如果是這樣,請(qǐng)僅對(duì)該單元格應(yīng)用不同的樣式。
我不知道是否有一種方法來(lái)確定第一小區(qū)的ListView,但我們一定可以捕捉在第一項(xiàng)List。
考慮以下應(yīng)用程序。我們有一個(gè)ListView只顯示字符串列表的。
我們?cè)O(shè)置自定義CellFactory的ListView,并設(shè)置單元格樣式,如果item是在第一List填充ListView。
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Simple interface
VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);
// Create the ListView
ListView<String> listView = new ListView<>();
listView.getItems().setAll("Title", "One", "Two", "Three", "Four", "Five");
// Set the CellFactory for the ListView
listView.setCellFactory(list -> {
ListCell<String> cell = new ListCell<String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
// There is no item to display in this cell, so leave it empty
setGraphic(null);
// Clear the style from the cell
setStyle(null);
} else {
// If the item is equal to the first item in the list, set the style
if (item.equalsIgnoreCase(list.getItems().get(0))) {
// Set the background color to blue
setStyle("-fx-background-color: blue; -fx-text-fill: white");
}
// Finally, show the item text in the cell
setText(item);
}
}
};
return cell;
});
root.getChildren().add(listView);
// Show the Stage
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
結(jié)果
顯然,您需要進(jìn)行一些調(diào)整以匹配您的數(shù)據(jù)模型,并且僅通過(guò) a 進(jìn)行匹配String
并不是最好的方法。
這不會(huì)阻止用戶選擇第一個(gè)項(xiàng)目,并且如果在構(gòu)建場(chǎng)景后對(duì)列表進(jìn)行排序,則可能無(wú)法按預(yù)期工作。
雖然這可能會(huì)回答您的直接問(wèn)題,但為了確保為用戶提供良好的體驗(yàn),還需要考慮其他事項(xiàng)。
添加回答
舉報(bào)