Android has added a new AsyncLayoutInflater class to their support library ver 24.0 and higher and can be used in Android SDK 4.0 or higher (which is almost all devices available).
As per Android’s documentation;
Helper class for inflating layouts asynchronously. To use, construct an instance of AsyncLayoutInflater on the UI thread and call inflate(int, ViewGroup, OnInflateFinishedListener). The AsyncLayoutInflater.OnInflateFinishedListener will be invoked on the UI thread when the inflate request has been completed.
This is intended for parts of the UI that are created lazily or in response to user interactions. This allows the UI thread to continue to be responsive & animate while the relatively heavy inflate is being performed.
I actually find this useful, as you may or may have not realized, inflating a complex view is an expensive action timewise, sometimes taking 100’s of milliseconds. Thus when it’s inflated on the Main Thread, the UI may become sluggish.
Another benefit I found, is the quicker load time for Activities, as the UI thread can continue with the next step in loading, while the view is being inflated separately. I have sometimes been able to significantly speed up App launch time using this method.
That being said, there are pitfalls that can happen when using a separate thread to inflate views. We will explain them in the answer.