如何使用ReactFX将ObservableList转换为Var?

时间:2018-06-26 11:13:20

标签: java javafx reactfx

我想将Var(双向)绑定到ObservableList到Var,如何实现呢?该代码演示了这个想法。

LiveList<String> list = new LiveArrayList<>();
list.addListener((ListChangeListener<? super String>) c ->
     System.out.println("source list changed: " + c.getList()));

// Val<List<String>> listVal = Val.create(() -> list, list);
    Val<List<String>> listVal = list.collapse(Function.identity());
listVal.values().subscribe(v -> System.out.println("listval changed :" + v));

Var<List<String>> listVar = Var.fromVal(listVal, list::setAll);
listVar.values().subscribe(v -> System.out.println("listvar changed :" + v));

list.add("e1");
list.add("e2");
list.add("e3");

但是它打印

listval changed :[]
listvar changed :[]
source list changed: [e1]
source list changed: [e1, e2]
source list changed: [e1, e2, e3]

我希望

listval changed :[]
listvar changed :[]
source list changed: [e1]
listval changed :[e1]
listvar changed :[e1]
source list changed: [e1, e2]
listval changed :[e1, e2]
listvar changed :[e1, e2]
source list changed: [e1, e2, e3]
listval changed :[e1, e2, e3]
listvar changed :[e1, e2, e3]

我想念什么?

0 个答案:

没有答案