AsyncLayoutInflater

public final class AsyncLayoutInflater


Helper class for inflating layouts asynchronously. To use, construct an instance of AsyncLayoutInflater on the UI thread and call inflate. The OnInflateFinishedListener will be invoked on the UI thread is no executor is passed, otherwise, it is called on the given executor.

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.

For a layout to be inflated asynchronously it needs to have a parent whose generateLayoutParams is thread-safe and all the Views being constructed as part of inflation must not create any Handlers or otherwise call myLooper. If the layout that is trying to be inflated cannot be constructed asynchronously for whatever reason, AsyncLayoutInflater will automatically fall back to inflating on the UI thread.

NOTE that the inflated View hierarchy is NOT added to the parent. It is equivalent to calling inflate with attachToRoot set to false. Callers will likely want to call addView in the OnInflateFinishedListener callback at a minimum.

This inflater does not support setting a LayoutInflater.Factory nor LayoutInflater.Factory2. Similarly it does not support inflating layouts that contain fragments.

Summary

Nested types

Public constructors

AsyncLayoutInflater(
    @NonNull Context context,
    @NonNull AsyncLayoutFactory asyncLayoutFactory
)

Public methods

void
@UiThread
inflate(
    @LayoutRes int resid,
    @Nullable ViewGroup parent,
    @NonNull AsyncLayoutInflater.OnInflateFinishedListener callback
)

Triggers view inflation on background thread.

void
@UiThread
inflate(
    @LayoutRes int resid,
    @Nullable ViewGroup parent,
    @Nullable Executor callbackExecutor,
    @NonNull AsyncLayoutInflater.OnInflateFinishedListener callback
)

Triggers inflation on a background thread.

Public constructors

AsyncLayoutInflater

Added in 1.0.0
public AsyncLayoutInflater(@NonNull Context context)

AsyncLayoutInflater

Added in 1.1.0-alpha02
public AsyncLayoutInflater(
    @NonNull Context context,
    @NonNull AsyncLayoutFactory asyncLayoutFactory
)

Public methods

inflate

Added in 1.0.0
@UiThread
public void inflate(
    @LayoutRes int resid,
    @Nullable ViewGroup parent,
    @NonNull AsyncLayoutInflater.OnInflateFinishedListener callback
)

Triggers view inflation on background thread.

inflate

Added in 1.1.0-alpha02
@UiThread
public void inflate(
    @LayoutRes int resid,
    @Nullable ViewGroup parent,
    @Nullable Executor callbackExecutor,
    @NonNull AsyncLayoutInflater.OnInflateFinishedListener callback
)

Triggers inflation on a background thread. It triggers the OnInflateFinishedListener on the given executor instead of the main thread.