8

I currently inflate most of my layouts using the DataBindingUtil.inflate(inflater, layoutId, parent, attachToParent) method.

But I saw that their is an AsyncLayoutInflater since Support Library revision 24 that allow the inflation to occur on a separate thread. I would like to use this mechanism in some part of my app but I don't want to drop the use of databinding for that.

DataBindingUtil doesn't contain any method like inflateAsync(). But is it plan to add a support for that? Or is their a way to combine both the AsyncLayoutInflater and the use of databinding?

I tried to use the AsyncLayoutInflater inside the inflate method of DataBindingUtil but actually AsyncLayoutInflater is not a subclass of the original LayoutInflater.

Thank's for reading!

MHogge
  • 5,408
  • 15
  • 61
  • 104

2 Answers2

14

You can just use DataBindingUtil.bind(view) to bind to the root of the inflated layout.

new AsyncLayoutInflater(this).inflate(R.layout.my_layout, null, new AsyncLayoutInflater.OnInflateFinishedListener() {
    @Override
    public void onInflateFinished(@NonNull View view, int resid, @Nullable ViewGroup parent) {
        MyLayoutBinding binding = DataBindingUtil.bind(view);
    }
});
James McCracken
  • 15,488
  • 5
  • 54
  • 62
1

There is no way to use the AsyncLayoutInflater with data binding, but my question would be, is it actually useful? Inflating a layout asynchronously can have its advantages, but if you're working with data binding, you run the risk of the view not being inflated when you attempt to bind data to the views, as there is no method such as waitForBinding().

WoogieNoogie
  • 1,258
  • 1
  • 11
  • 21
  • yes its pretty useful when you build complex UI you would not appreciate it if you only build simple UI. You can pretty much make a simple loading ui while the complex ui is loading asynchronously. – Renz Carlo Jan 25 '23 at 23:34