Lately I've been using data bindings and I came across the executePendingBindings
method.
The documentation doesn't say much about it, so I don't understand how it works or when to use it. Here is an example of the method's usage.
Please give an example that demonstrates the difference of using it and not. Thank you
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
Customer customer= List.get(position).second;
((CustomerViewHolder)holder).binding.setCustomer (customer)
((CustomerViewHolder)holder).binding.executePendingBindings();
}
For a more detailed explanation you can refer to this example.
When not using executePendingBindings()
your list will flicker when you bind it, for example when you open a new list you will notice this flicker / jitter effect, this is because the list is populated and then in the next frame it's bound.
If you don't want this to happen and execute immediately, your binding this method will prevent the flickering and bind your data right away.
executePendingBindings()
If you see, there is a flickering effect since the views are initialized, then data binded and then in the next frame the view binding happens.
executePendingBindings()
If you see here, there is no flickering effect, you can see that the list is binded normally and works as one go.
You can only use executePendingBindings()
on the UI thread, this means that in the time onBindViewHolder
is called, you will need to use it on the binding, doing so will guarantee that you are calling it on the UI.
override fun onBindViewHolder(binding: MyBindingClass, position: Int, viewType: Int) {
//Your binding code
binding.executePendingBindings()
}
Always call it at the end of the onBindViewHolder
executePendingBindings()
function is for immediate binding.
When a variable or observable object changes, the binding is scheduled to change before the next frame. There are times, however, when binding must be executed immediately. To force execution, use the executePendingBindings() method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With