Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android data binding lags when rendering

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:

  • new search term entered: list cleared, items removed [OK]
  • response arrives back from server [OK]
  • I add all items to the list and notify adapter about the change [OK]
  • for a fraction of a second the old values are appearing in the list and list items will be updated with the new values only after that [NOT OK]

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

like image 629
ktamas Avatar asked Oct 26 '25 23:10

ktamas


1 Answers

call

mBinding.executePendingBindings ();

just after mBinding.setData(data);

like image 160
Blackbelt Avatar answered Oct 28 '25 15:10

Blackbelt