Issue
I have been using lately data bindings and i came across the executePendingBindings
method. The documentation shows little much about it and i can't understand how it works or when to use it.
A lot of developers use the executePendingBindings inside the onBindViewHolder callback but i don't see any differences myself in recycler when using it or not.
Can someone explain why its important to use in recycler?? Thanks
@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();
}
Solution
Doing some changes on your binding does not mean that it will have an immediate effect on your View
. Changing things in binding means that you're really scheduling those changes to be applied in the nearest future. This is for many reasons, performance being one of them.
Imagine you've got some complex expressions in your xmls. You don't want to figure out everything related to binding variables before you set all of them. That would be wasting of resources.
You can see more about it in the generated binding java class itself. I suggest you read through it.
Calling executePendingBindings
means that you're essentially forcing the framework to do everything it needs to do so far on the binding, right at the moment of calling it.
You don't have to do it in your Adapter if your case does not require that. Some people are doing that to be sure that everything is properly set on an item before moving on. So e.g. there's no case like the onBind
being called again before the previous round of bindings was executed... or something similar...
EDIT 1:
Also... don't forget that scheduling of executing changes (for the nearest future) is the thing that allows you to setVariables
on binding
from threads different than UI
thread. Because setting a variable does not touch the View
itself.
EDIT 2:
The easiest way to look at the generated java classes is to:
- Navigate -> File
- Type the name of your binding file in UpperCamelCase followed by
Binding
(e.g. if your layout isactivity_main
, typeActivityMainBinding
)
Answered By - Bartek Lipinski
Answer Checked By - Mildred Charles (JavaFixing Admin)