9

I am looking for a practical example for both the cases that what to use when? I have seen similar threads but they only tell this "when binding must be executed immediately" but there is no real time example for any case where you have to force the binding to be executed. So please if anyone can explain with any example that when to use it and when its not that required!

2

1 Answer 1

13

The case when you should use executePendingBindings() is (but there could be more):

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.

In case of a RecyclerView, this happens if:

  1. You have multiple viewTypes with different sizes (different XML)
  2. Your row height changes based on view contents.

RecyclerView will measure the row size after the onBindViewHolder has completed. If based on the data you set in this method the height of the row changes, the measurement could not take into account the extra or reduced space occupied by your new content if you do not call executePendingBindings().

Forcing the binding to update data synchronously and not in the next frame keeps you safe from wrong row size measurement (and potentially missing content)

2
  • 1
    Yeah pretty much what I was looking for in an answer. However, does that mean if there are 500 elements in a list all elements will be subjected to this UI change?
    – sanjeev
    Commented Apr 30, 2021 at 11:44
  • 3
    onBindViewHolder should be called only for visible elements (or near visible). Only those elements are affected by executePendingBindings
    – MatPag
    Commented Apr 30, 2021 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.