css样式适用于AppBar为时已晚

时间:2017-06-27 02:13:18

标签: gluon gluon-mobile

我有string region = Region.AsiaEast.ToString(); ,其中包含View。 第一次加载视图时,创建ListCells需要一些时间。为防止用户界面被阻止,我将调用包裹在ListView中的listView.setItems,并将placeHolder设置为listView作为加载过程的指示器。
视图Platform.runLater包含AppBar和一些TextField,它们通过css-file设置样式。虽然按钮的样式正确,但textField首先出现时没有应用任何样式,即黑色,一旦填充了ListView,TextField也会应用其样式。

这种延迟可能是什么原因?

Buttons

编辑:

只有-fx-text-fill在延迟后应用。所有其他样式立即应用。

    @Override
    protected void onShowing() {
        appBar.setNavIcon(getBackButton());
        appBar.setTitle(searchField.getTextField());
        appBar.getActionItems().addAll(btnSort, btnFilter, moreMenu);
    }

    @Override
    protected void onShown() {
        setItems();
    }

    protected void setItems() {
        if (!firstShow) {
            lview.setItems(items);

        } else {
            if (!items.isEmpty()) {
                lview.setPlaceholder(new Label("loading data..."));
                Platform.runLater(() -> lview.setItems(items));
            }
            firstShow = false;
        }
    }

重现问题的代码:

.app-bar > .title-box > .text-field{ 
    -fx-border-width: 0.0; 
    -fx-border-insets: 0.0;
    -fx-padding: 0.0;
    -fx-font-family: 'Roboto Medium';
    -fx-font-size: 20.0;
    -fx-text-fill: -app-bar-item-color;
    -fx-prompt-text-fill: #59819C;
}

.app-bar > .action-items .button{
    -fx-text-fill: red;
}

1 个答案:

答案 0 :(得分:0)

发布代码后,我可以在Android上重现该问题。

显然,我接受了这一部分:

private void onShown() {
    lview.setPlaceholder(new Label("loading data..."));
    Platform.runLater(() -> {
        try {
            Thread.sleep(2000);  //to simulate a long lasting initial creation of listCells
        } catch (InterruptedException e) { }
        lview.setItems(items);
    });
}
添加

以模拟单元格的初始创建。

但正是它显示了您面临的问题:模拟示例显示在JavaFX线程(Platform.runLater)上,您阻止了它(Thread.sleep(2000))。在此期间,textField文本为黑色,稍后才会对其进行样式设置。

如果您将该片段修改为此片段:

private void onShown() {
    lview.setPlaceholder(new Label("loading data..."));
    new Thread(new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            try {
                Thread.sleep(2000);  //to simulate a long lasting initial creation of listCells
            } catch (InterruptedException e) { }
            return null;
        }

        @Override
        protected void succeeded() {
            lview.setItems(items);
        }
    }).start();
}

现在繁重的任务在JavaFX线程之外,你可以看到textField是立即设置的。

回到你真正的问题:这里的教训可能是你应该在每次繁重的任务中移出JavaFX线程,并且只在最后一刻调用它。 Task是一个很好的机制。

相关问题