I am using Android Data Binding with a RecyclerView.Adapter.
On Adapter's onCreateViewHolder I call:
public TransfersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHolder(
ListItemMyDataBinding.inflate(
LayoutInflater.from(parent.getContext()),
parent,
false));
}
So ListItemMyDataBinding is a representation of list_item_my_data.xml.
Adapter's onBindViewHolder looks something like this:
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
MyData myData = mDataList.get(position);
holder.render(myData);
....
}
And finally, the render function of my MyViewHolder looks like this:
public void render(MyData data, boolean expand) {
mBinding.setData(data); // mBinding is an instance of ListItemMyDataBinding
}
And in my xmls there are some data bindings:
<data>
<import type="com.example.MyData" />
<variable
name="data"
type="MyData" />
</data>
...
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{data.userName}" />
I use this approach in a searchable list, so I have a search field on top of the list. Once a user enters a new search term, list is cleared and notified about change, once the response comes back from server I add all results to the list and notify about the change again.
And finally the problem is that the following happens:
As I understand, this is because the Views are reused and as a first step the adapter realises that it can reuse a view and displays it, and only (a fraction of seconds) after it applies the changes in the layout (through databinding), so that's why I see the old values blinking up. Which is quite annoying.
If in function render I manually set a default value to the views (for example setText("") on TextViews) then I cannot see that blinking effect, but this is not a solution that I want.
Do you have any suggestions how can I nicely avoid this "blinking/lag" effect, what am I doing wrong?
Thanks
call
mBinding.executePendingBindings ();
just after mBinding.setData(data);
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