第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

JavaFX ListCell updateItem 執(zhí)行兩次?

JavaFX ListCell updateItem 執(zhí)行兩次?

九州編程 2023-03-23 16:16:26
我正在嘗試在 ListView 中創(chuàng)建自定義單元格,但每次我添加一個(gè)新項(xiàng)目時(shí), updateItem (TextFlow item, Boolean empty)都會(huì)執(zhí)行兩次:一次它收到 null和true,第二次它沒(méi)有(!空和假)如果我不實(shí)施setCellFactory方法,那么我可以毫無(wú)問(wèn)題地將項(xiàng)目添加到表中。沒(méi)有自定義 cellFactory 的ListView但是,當(dāng)我實(shí)施它時(shí),它只是創(chuàng)建了 10 個(gè)空單元格(內(nèi)容在哪里?)。帶有自定義 cellFactory 的ListViewpublic class Controller implements Initializable {@FXMLprivate ListView <TextFlow> console;private ObservableList<TextFlow> data = FXCollections.observableArrayList();public void initialize(URL location, ResourceBundle resources) {    console.setCellFactory(new Callback<ListView<TextFlow>, ListCell<TextFlow>>() {        @Override        public ListCell<TextFlow> call(ListView<TextFlow> param) {            return new ListCell<TextFlow>() {                @Override                protected void updateItem(TextFlow item, boolean empty) {                    super.updateItem(item, empty);                    if (item != null) {                        setItem(item);                        setStyle("-fx-control-inner-background: blue;");                    } else {                        System.out.println("Item is null.");                    }                }            };        }    });    for (int i = 0 ; i < 10; i++) {        Text txt = getStyledText("This is item number " + i + ".");        TextFlow textFlow = new TextFlow();        textFlow.getChildren().add(txt);        data.add(textFlow);    }    console.setItems(data);}private Text getStyledText (String inputText) {    Text text = new Text(inputText);    text.setFont(new Font("Courier New",12));    text.setFill(Paint.valueOf("#000000"));    return text;}}
查看完整描述

1 回答

?
白板的微信

TA貢獻(xiàn)1883條經(jīng)驗(yàn) 獲得超3個(gè)贊

updateItem可以調(diào)用任意次數(shù),可以傳遞不同的項(xiàng)目,單元格可以從空變?yōu)榉强?,反之亦然。ListView創(chuàng)建與您在屏幕上看到的一樣多的單元格,并用項(xiàng)目填充它們。例如,滾動(dòng)或修改列表items或調(diào)整大小ListView可以導(dǎo)致更新。


出于這個(gè)原因,任何單元格都需要能夠處理傳遞null給該方法的任意項(xiàng)目序列(或+空)updateItem。


此外,您應(yīng)該避免調(diào)用setItem自己,因?yàn)閟uper.updateItem已經(jīng)這樣做了。setGraphic如果要在單元格中顯示項(xiàng)目,請(qǐng)改用:


@Override

public ListCell<TextFlow> call(ListView<TextFlow> param) {

    return new ListCell<TextFlow>() {

        @Override

        protected void updateItem(TextFlow item, boolean empty) {

            super.updateItem(item, empty);


            if (item != null) {

                setStyle("-fx-control-inner-background: blue;");

                setGraphic(item);

            } else {

                setStyle(null);

                setGraphic(null);

                System.out.println("Item is null.");

            }


        }

    };

}


查看完整回答
反對(duì) 回復(fù) 2023-03-23
  • 1 回答
  • 0 關(guān)注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)