TornadoFX - 如何修复带有自定义单元工厂的 ListView 在删除项目时未正确更新?
TornadoFX - how to fix ListView with a custom cell factory not updating properly when items are deleted?
我有一个 ListView
显示来自我的域模型的自定义对象,如果我使用自定义单元工厂在列表的每一行中显示对象的属性,我在删除项目时会出现奇怪的行为.如果该项目不是列表中的最后一项,则删除的项目仍然可见,最后一项消失。但是,该项目已按预期从后备列表中删除,尝试删除幻影对象没有进一步的影响。
显示似乎没有正确刷新,因为在 window 任意调整大小后,列表最终刷新到其预期值。我试过在 ListView
上手动调用 refresh()
,但效果不明显。
删除我的自定义单元工厂解决了这个问题,我看到其他帖子使用标准 JavaFX () 也有类似的问题,其中通过更改 [=14] 的实现解决了这个问题=],但我不知道如何在 TornadoFX 中做到这一点。
这是一个演示更新问题的示例(但不是幻影项目,只有当删除按钮是自定义单元格的一部分时才会发生):
package example
import javafx.scene.control.ListView
import tornadofx.*
data class DomainClass(val name: String, val flag1: Boolean, val flag2: Boolean, val info: String)
class UpdateIssue : App(UpdateIssueView::class)
class UpdateIssueView : View() {
val listSource = mutableListOf(
DomainClass("object1", true, false, "more info"),
DomainClass("object2", false, true, "even more info"),
DomainClass("object3", false, false, "all the info")
).observable()
var lst: ListView<DomainClass> by singleAssign()
override val root = vbox {
lst = listview(listSource) {
cellFormat {
graphic = cache {
hbox {
textfield(it.name)
combobox<Boolean> {
selectionModel.select(it.flag1)
}
combobox<Boolean> {
selectionModel.select(it.flag2)
}
textfield(it.info)
}
}
}
}
button("delete") {
action {
listSource.remove(lst.selectedItem)
}
}
}
}
非常感谢任何帮助!
@Edvin Syse 提出的删除缓存块的建议为我解决了这个问题(尽管请注意,他还说更高效的解决方法是实施 ListCellFragment
,我在这里没有这样做) :
....
lst = listview(listSource) {
cellFormat {
graphic = hbox {
textfield(it.name)
combobox<Boolean> {
selectionModel.select(it.flag1)
}
combobox<Boolean> {
selectionModel.select(it.flag2)
}
textfield(it.info)
}
}
}
我注意到 ComboBoxes 除了 it.flag1 和 flag2 之外没有显示任何其他可选值。您需要将值 属性 设置为 true/false 或 true/false/null。然后就可以直接设置值项了。
lst = listview(listSource) {
cellFormat {
graphic = hbox {
textfield(it.name)
combobox(values=listOf(true, false)) {
value = it.flag1
}
combobox(values=listOf(true, false)) {
value = it.flag2
}
textfield(it.info)
}
}
}
我有一个 ListView
显示来自我的域模型的自定义对象,如果我使用自定义单元工厂在列表的每一行中显示对象的属性,我在删除项目时会出现奇怪的行为.如果该项目不是列表中的最后一项,则删除的项目仍然可见,最后一项消失。但是,该项目已按预期从后备列表中删除,尝试删除幻影对象没有进一步的影响。
显示似乎没有正确刷新,因为在 window 任意调整大小后,列表最终刷新到其预期值。我试过在 ListView
上手动调用 refresh()
,但效果不明显。
删除我的自定义单元工厂解决了这个问题,我看到其他帖子使用标准 JavaFX (
这是一个演示更新问题的示例(但不是幻影项目,只有当删除按钮是自定义单元格的一部分时才会发生):
package example
import javafx.scene.control.ListView
import tornadofx.*
data class DomainClass(val name: String, val flag1: Boolean, val flag2: Boolean, val info: String)
class UpdateIssue : App(UpdateIssueView::class)
class UpdateIssueView : View() {
val listSource = mutableListOf(
DomainClass("object1", true, false, "more info"),
DomainClass("object2", false, true, "even more info"),
DomainClass("object3", false, false, "all the info")
).observable()
var lst: ListView<DomainClass> by singleAssign()
override val root = vbox {
lst = listview(listSource) {
cellFormat {
graphic = cache {
hbox {
textfield(it.name)
combobox<Boolean> {
selectionModel.select(it.flag1)
}
combobox<Boolean> {
selectionModel.select(it.flag2)
}
textfield(it.info)
}
}
}
}
button("delete") {
action {
listSource.remove(lst.selectedItem)
}
}
}
}
非常感谢任何帮助!
@Edvin Syse 提出的删除缓存块的建议为我解决了这个问题(尽管请注意,他还说更高效的解决方法是实施 ListCellFragment
,我在这里没有这样做) :
....
lst = listview(listSource) {
cellFormat {
graphic = hbox {
textfield(it.name)
combobox<Boolean> {
selectionModel.select(it.flag1)
}
combobox<Boolean> {
selectionModel.select(it.flag2)
}
textfield(it.info)
}
}
}
我注意到 ComboBoxes 除了 it.flag1 和 flag2 之外没有显示任何其他可选值。您需要将值 属性 设置为 true/false 或 true/false/null。然后就可以直接设置值项了。
lst = listview(listSource) {
cellFormat {
graphic = hbox {
textfield(it.name)
combobox(values=listOf(true, false)) {
value = it.flag1
}
combobox(values=listOf(true, false)) {
value = it.flag2
}
textfield(it.info)
}
}
}