Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 366
sak Avatar asked Nov 03 '19 09:11

sak


People also ask

What is executePendingBindings?

executePendingBindings() Evaluates the pending bindings, updating any Views that have expressions bound to modified variables. View. getRoot() Returns the outermost View in the layout file associated with the Binding.

Which is the correct way to reference bound data in the XML layout?

Layout Binding expressions Expressions in the XML layout files are assigned to a value of the attribute properties using the “ @{} " syntax. We just need to use the basic syntax @{} in an assignment expression.

What does binding inflate do?

inflate. Inflates a binding layout and returns the newly-created binding for that layout. Use this version only if layoutId is unknown in advance. Otherwise, use the generated Binding's inflate method to ensure type-safe inflation.


1 Answers

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)

like image 83
MatPag Avatar answered Oct 08 '22 08:10

MatPag