数据绑定方法何时在布局后触发 Inflation?

When Do Data Binding Methods Fire After Layout Inflation?

我正在使用数据绑定,但我 运行 遇到了一个问题,即我绑定到的视图 属性 在布局膨胀并且视图模型是为绑定 class.

设置

在包含的 Fragment 中,我使用 BindingUtil 在 Fragment 的 onCreateView() 中扩充绑定布局。然后我立即设置绑定变量,这是我的视图模型。

但是,当我在设置绑定的视图模型后立即进行调试时,布局对我绑定到视图模型的某些视图属性具有空值。如果我等待几分之一秒,将按预期通过绑定设置属性。

因此,视图属性的实际绑定何时发生?我假设它在视图模型设置为绑定 class 时立即发生。这一定是错的...

有人可以帮我解决这个问题吗?

根据 Data Binding documentation:

When a variable or observable changes, the binding will be scheduled to change before the next frame. There are times, however, when binding must be executed immediately. To force execution, use the executePendingBindings() method.