我提出了以下解决方案:添加事件侦听器 - 每次添加、删除或替换元素时调整 ListView 的高度。调整大小时检查是否只有垂直和/或水平滚动条。如果仅存在垂直滚动条,则使用其高度和插图来获取单元格高度。如果还存在水平滚动条,则也添加其高度(拇指高度)。以下是相关的代码片段(在 Scala 中):
def extendHeight(thumbnails: ListView[String], vertical: Option[ScrollBar], horizontal: Option[ScrollBar]): Unit = {
(vertical, horizontal) match {
case (None, None) => ()
case (None, Some(_)) => ()
case (Some(v), None) =>
resizeHeight(thumbnails, v, 0)
case (Some(v), Some(h)) =>
val extra = if (h.isVisible) h.getHeight else 0
resizeHeight(thumbnails, v, extra)
}
}
def resizeToFrame(thumbnails: ListView[String]): Unit = {
val (vertical, horizontal) = PStoryBoardUX.getScrollBars(thumbnails)
PStoryBoardUX.extendHeight(thumbnails, vertical, horizontal)
}
thumbnails.getItems.addListener( new ListChangeListener[String] {
override def onChanged(c: Change[_ <: String]): Unit = {
javafx.application.Platform.runLater( () => {
// At thus point in time the scrollbars have not been updated
// So we don't know if they will be active or not. Force this
thumbnails.layout()
// Now resize to remove the visible scrollbars
resizeToFrame(thumbnails)
})
}
})