有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java隐藏javafx Listview中的垂直滚动条

我已经试了好几个小时了,似乎到处都找不到答案。我有两个问题与同一个listview有关,一个比另一个更严重

我正在尝试匹配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";   
}

简言之,我已经成功地移除了滚动条,但滚动条消失后占据的空间似乎仍被占用,我无法让红色的滚动条显示在牢房的最末端

最初的设计灵感来自安卓 listview,其中滚动条位于内容顶部,滚动时拇指是半透明的,不滚动时拇指是不可见的。理想情况下,我也希望实现这一点,尽管如果可以将红线放在末尾,滚动条将不会是一个可接受的折衷方案

我曾经尝试过使用滚动窗格和标准AnchorPane代替ListView,但都没有效果,于是决定使用ListView再试一次。但我愿意接受任何能够达到预期效果的解决方案,即使这意味着再次撕毁ListView

如果您能给予任何帮助,我们将不胜感激

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

谢谢


共 (1) 个答案

  1. # 1 楼答案

    改变

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

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

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

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