简体   繁体   English

在javafx Listview中隐藏垂直滚动条

[英]Hide vertical scrollbar in javafx Listview

I've tried for several hours now and can't seem to find the answer anywhere. 我已经尝试了几个小时,似乎无法在任何地方找到答案。 I have 2 issues relating to the same listview, 1 more serious than the other. 我有2个与同一列表视图相关的问题,1个比另一个更严重。

I am trying to match a design provided to me in java. 我试图匹配java中提供给我的设计。 The list view should look like the below 列表视图应如下所示

This is from the design 这是从设计

Currently I have got as far as 目前我已经到了

My current endeavour 我目前的努力

I have created a Custom ListCell to hold all of the views (not yet refactored so may look a little untidy) 我创建了一个自定义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);            
        }
    }

}

and my CSS is 我的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";   
}

In a nutshell I have managed to remove the scrollbar but the space the scrollbar occupied still seems to be taken up after it has disappeared and I can't for the life of me get the red bar to show at the very end of the cell. 简而言之,我已经设法移除了滚动条,但滚动条占用的空间似乎仍然在它消失之后被占用,我不能在我的生活中获得红色条显示在单元格的最后。

The original design had inspiration from an android listview where the scrollbar is placed on top of the content and the thumb is semi-opaque when scrolling and invisible when not. 原始设计灵感来自Android列表视图,其中滚动条放置在内容的顶部,拇指在滚动时是半透明的,而在不滚动时是不可见的。 I would ideally like to achieve this as well although no scrollbar would be an acceptable compromise if the red line can be placed at the end. 理想情况下我也希望实现这一点,但如果红线可以放在最后,没有滚动条是可接受的折衷方案。

I have explored using a ScrollPane and standard AnchorPane's instead of a ListView to no avail and decided to try again with the ListView. 我已经探索过使用ScrollPane和标准的AnchorPane而不是ListView无济于事,并决定再次使用ListView。 But i'm open to any solution that achieves the desired even if this means ripping up the ListView again. 但我愿意接受任何达到预期效果的解决方案,即使这意味着再次撕掉ListView。

Any help you could give would be appreciated. 任何你能给予的帮助将不胜感激。

sorry if my question is incorrectly formatted i'm a newbie :) 对不起,如果我的问题格式不正确,我是新手:)

Thanks 谢谢

Change 更改

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

to

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

And if you want disable move between items: 如果你想在项目之间禁用移动:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM