何时使用 executePendingBindings() 以及何时不需要?

When to use executePendingBindings() and when its not required?

我正在为这两种情况寻找一个实际的例子,什么时候使用什么?我见过类似的线程,但他们只告诉这个“何时必须立即执行绑定”,但没有任何实时示例说明您必须强制执行绑定。所以,如果有人能用任何例子来解释什么时候使用它,什么时候不需要它,请多多指教!

你应该使用 executePendingBindings() 的情况是(但可能还有更多):

Every time the binding update could cause a View to change its size and postponing the calculation in the next frame could cause the measurement to read wrong values.

RecyclerView 的情况下,如果:

  1. 您有多个 viewType 大小不同(不同 XML)
  2. 您的行高根据视图内容而变化。

RecyclerView 将在 onBindViewHolder 完成后测量行大小。如果根据您在此方法中设置的数据,行高发生变化,则如果您不调用 executePendingBindings(),则测量无法考虑新内容占用的额外或减少的 space。 =17=]

强制绑定同步更新数据而不是在下一帧中更新数据可以避免错误的行大小测量(以及可能丢失的内容)