问题描述
如果我创建一个ListView:
If I create a ListView:
new ListView<>(FXCollections.observableArrayList("1", "2", "3"))
我希望它创建一个具有3行的ListView.但事实并非如此.它创建一个17行左右的ListView.有没有办法告诉ListView始终保持高度,以便始终显示其中的任何项目但没有空白行?
I would expect it to create a ListView with 3 rows. But it doesn't. It creates a ListView of 17 or so rows. Is there a way to tell ListView to always be the height such that whatever items are in it are always shown but no blank rows?
具有自动宽度也很有用,因此它总是与最宽的行一样宽.
Having it auto width would also be useful, so it's always as wide as the widest row.
这样做的目的之一是可以在ScrollPane中使用它.我知道它有自己的滚动条,但是它们没有提供足够的控制.
One purpose of this is that then it could be used in a ScrollPane. I know it has its own scrollbars, but they don't offer sufficient control.
推荐答案
我刚刚发现,可以使用 Bindings.size ,它创建一个数字jfx属性来表示ObservableList的大小:
I just found out that Paul Marshall's answer can be reducted to a one-liner using Bindings.size that creates a numeric jfx property representing the size of an ObservableList :
listView.prefHeightProperty().bind(Bindings.size(itemListProperty).multiply(LIST_CELL_HEIGHT));
可悲的是,列表单元格的高度仍必须是硬编码的AFAIK.
The list cell height must sadly still be hardcoded AFAIK.
这篇关于如何使JavaFX ListView成为其项目的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!