在javafx Listview中隐藏垂直滚动条

Dan*_*iel 4 java listview javafx

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

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

这是从设计

目前我已经达到

我目前的努力

我创建了一个Custom 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);            
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

而我的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";   
}
Run Code Online (Sandbox Code Playgroud)

简而言之,我设法删除了滚动条,但是滚动条消失后似乎仍然占据了滚动条所占用的空间,我一生无法让红色条显示在单元格的最后。

原始设计的灵感来自android列表视图,其中滚动条位于内容的顶部,滚动时拇指半透明,不滚动时拇指不可见。我理想地也希望实现这一点,尽管如果可以将红线放在末尾,则不会有任何滚动条成为可接受的折衷方案。

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

您能提供的任何帮助将不胜感激。

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

谢谢

小智 5

更改

.list-view .scroll-bar:vertical {
   -fx-padding: 0px;
}
Run Code Online (Sandbox Code Playgroud)

.list-view .scroll-bar:vertical {
    -fx-scale-x: 0;
}
Run Code Online (Sandbox Code Playgroud)

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

listview.setMouseTransparent( true );
listview.setFocusTraversable( false );
Run Code Online (Sandbox Code Playgroud)

  • 这对我来说不太管用,我还有酒吧的背景。我能够通过使用 -fx-padding: -10; 修复它 -fx-不透明度:0; (2认同)