【问题标题】:Hide vertical scrollbar in javafx Listview在javafx Listview中隐藏垂直滚动条
【发布时间】:2017-02-08 08:32:09
【问题描述】:

我已经尝试了几个小时,但似乎无法在任何地方找到答案。我有 2 个与同一个列表视图相关的问题,其中 1 个比另一个更严重。

我正在尝试匹配在 java 中提供给我的设计。列表视图应如下所示

This is from the design

目前我已经做到了

My current endeavour

我创建了一个自定义 ListCell 来保存所有视图(尚未重构,因此可能看起来有点凌乱)

import HolderClasses.MenuItem;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;

public class CustomListCell extends ListCell<MenuItem>{

    private Image mImage;
    private String mText;
    private AnchorPane background;
    private ImageView imageHolder;
    private VBox colourStrip;
    private Label mTextLabel;

    public CustomListCell(){

        background = new AnchorPane();
        imageHolder = new ImageView();
        mTextLabel = new Label();
        imageHolder.setFitHeight(40.0);
        imageHolder.setFitWidth(40.0);
        imageHolder.getStyleClass().add("listCellImage");
        mTextLabel.getStyleClass().add("listCellText");
        AnchorPane.setLeftAnchor(imageHolder, 10.0);
        AnchorPane.setTopAnchor(imageHolder, 10.0);
        AnchorPane.setBottomAnchor(imageHolder, 10.0);
        AnchorPane.setLeftAnchor(mTextLabel, 80.0);
        AnchorPane.setRightAnchor(mTextLabel, 1.0);
        AnchorPane.setTopAnchor(mTextLabel, 0.0);
        AnchorPane.setBottomAnchor(mTextLabel, 0.0);
        colourStrip = new VBox();
        colourStrip.setMinWidth(0.0);
        colourStrip.setMaxWidth(2.0);
        colourStrip.setPrefWidth(2.0);
        colourStrip.getStyleClass().add("listCellColourStrip");
        AnchorPane.setRightAnchor(colourStrip, 0.0);
        AnchorPane.setTopAnchor(colourStrip, 0.0);
        AnchorPane.setBottomAnchor(colourStrip, 0.0);

        background.getChildren().addAll(mTextLabel, imageHolder, colourStrip);        

    }

    @Override
    protected void updateItem(MenuItem item, boolean empty) {
        super.updateItem(item, empty);
        if (empty || item == null) {
            //remove all the views
            setText(null);
            setGraphic(null);
        } else {
            //set all the views
            imageHolder.setImage(item.getImage());
            mTextLabel.setText(item.getText());
            setText(null);  
            setGraphic(background);            
        }
    }

}

我的 CSS 是

.list-cell {

    -fx-background-color: #FCFCFC;
    -fx-background-insets: 0 0 1 0;
}

.list-cell:empty {
    -fx-background-color: #FCFCFC;
    -fx-background-insets: 0 ;
}

.list-cell:selected .listCellColourStrip{

    -fx-background-color: #CF000F;

}

.list-cell:selected {

    -fx-background-color: #FFFFFF;

}

.list-view .scroll-bar:vertical .increment-arrow,
.list-view .scroll-bar:vertical .decrement-arrow,
.list-view .scroll-bar:vertical .increment-button,
.list-view .scroll-bar:vertical .decrement-button {
    -fx-padding: 0px;
}

.list-view .scroll-bar:vertical {
    -fx-padding: 0px;
}

.listCellImage {    
    -fx-opacity: 0.4;    
}

.listCellText{    
    -fx-font-size: 1.5em;
    -fx-text-fill: #888;
    -fx-font-family: "Museo Sans 500";   
}

简而言之,我已经设法删除了滚动条,但滚动条占用的空间在它消失后似乎仍然被占用,我终生无法在最后显示红色条细胞。

最初的设计灵感来自一个 android 列表视图,其中滚动条放置在内容的顶部,滚动时拇指是半透明的,不滚动时是不可见的。理想情况下,我也希望实现这一点,但如果可以将红线放在末尾,则没有滚动条是可以接受的折衷方案。

我已经探索过使用 ScrollPane 和标准 AnchorPane,而不是 ListView,但无济于事,并决定再次尝试使用 ListView。但我愿意接受任何能够达到预期的解决方案,即使这意味着再次撕毁 ListView。

如果您能提供任何帮助,我们将不胜感激。

对不起,如果我的问题格式不正确,我是新手 :)

谢谢

【问题讨论】:

  • 如何将您的内容放在滚动窗格内的 VBox 中?然后,您可以设置滚动条策略以使其可见或不可见。

标签: java listview javafx


【解决方案1】:

改变

.list-view .scroll-bar:vertical {
   -fx-padding: 0px;
}

.list-view .scroll-bar:vertical {
    -fx-scale-x: 0;
}

如果你想禁用项目之间的移动:

listview.setMouseTransparent( true );
listview.setFocusTraversable( false );

【讨论】:

  • 这对我来说不太适用,我仍然有酒吧的背景。我能够通过使用 -fx-padding: -10; 来修复它-fx-不透明度:0;
猜你喜欢
  • 2013-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-12
  • 2020-01-13
  • 1970-01-01
相关资源
最近更新 更多