ListProperty<T>与ObjectProperty<ObservableList<T>>

我注意到itemsProperty inListView的类型是ObjectProperty<ObservableList<T>>,但我早就预料到了ListProperty<T>

现在我想知道:我应该什么时候使用ObjectProperty<ObservableList<T>>,什么时候使用ListProperty<T>

回答

答案可以在ObjectProperty的 JavaFX 文档中找到,特别是这一行:

对于 ObservableList、ObservableSet 和 ObservableMap也报告集合内部变化的特殊实现,请分别参见 ListProperty、SetProperty 和 MapProperty。

因此,使用ObjectProperty<ObservableList<T>>如果将报告ObservableList本身的变化,但如果任何成员ObservableList发生变化。

因此,要回答您的问题,您将使用ObjectProperty<ObservableList<T>>when 您只需要在ObservableList已更改的情况下报告(即:添加或删除的项目)。ListProperty<T>如果您还需要跟踪对其中的实际项目所做的更改,则应该使用ObservableList

  • beware: _will report if the ObservableList itself changes_ that's true, with a catch - it fires invalidation events always, but change events only if `!newList.equals(oldList)` The latter is nasty if client code needs to re-wire listeners (as f.i. skins of virtualized controls) - your suggestion to use ListProperty in these contexts is full on 🙂

以上是ListProperty&lt;T&gt;与ObjectProperty&lt;ObservableList&lt;T&gt;&gt;的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>