英文:
TablewView setAll of backing List resets selection but not focus
问题
我正在开发一个类似任务管理器的应用程序,在这个应用程序中,TableView 的整个后备列表会被定期运行的命令行程序的结果所替代。我使用 setAll
将表格视图设置为最新的项目。虽然它能够工作,但它会重置视图中的选择。
有趣的是,蓝色焦点条并不会移动,但在使用 setAll
更新表格视图后,第一个项目上会显示一个非常小的轮廓。我猜这就是所选索引的呈现方式。当我使用箭头键导航时,选择会定期跳回列表的第一个项目。
有人知道如何解决这个问题吗?
英文:
I'm working on a Task-Manager like application, where the whole backing list of a TableView gets replaced by the result of periodically running a command line program. I'm using setAll
to set the table view to the newest items. It works, but it resets the selection in the view.
Interestingly, the blue focus bar does not move, but a very small outline is shown on the first item of the table view after it was updated with setAll
. I guess this is how the selected index is rendered. When I use the arrow keys to navigate, the selection will jump periodically back to the first item of the list.
Does anybody know how to solve this problem?
答案1
得分: 1
我找到了一种方法来做这件事,但我确信有更好的方法。我手动计算了新状态与先前状态之间的差异。然后我分别调用了 removeAll 和 addAll 方法:
object PortBindingLock
private fun reloadBindings() {
synchronized(PortBindingLock) {
val previous = this.portBindings.toSet()
val current = processService.processPortBindings().toSet()
val toRemove = Sets.difference(previous, current)
val toAdd = Sets.difference(current, previous)
this.portBindings.removeAll(toRemove)
this.portBindings.addAll(toAdd)
}
}
英文:
I found a way to do it, but I'm sure there is a better way. I manually calculate the difference between the new and the previous state. Then I call the removeAll, and addAll methods respectively:
object PortBindingLock
private fun reloadBindings() {
synchronized(PortBindingLock) {
val previous = this.portBindings.toSet()
val current = processService.processPortBindings().toSet()
val toRemove = Sets.difference(previous, current)
val toAdd = Sets.difference(current, previous)
this.portBindings.removeAll(toRemove)
this.portBindings.addAll(toAdd)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论