Inflate a view in a background thread

后端 未结 3 598
温柔的废话
温柔的废话 2020-12-01 09:20

I have a very simple question:

Is or is it not possible to inflate a view (not add it to layout) in a background thread (ex: in the doInBack

3条回答
  •  遥遥无期
    2020-12-01 09:31

    With latest support lib you can use android.support.v4.view.AsyncLayoutInflater to inflate views asynchronously. Be careful though that it can fallback to inflating on UI thread if specific requirements are not met:

    For a layout to be inflated asynchronously it needs to have a parent whose generateLayoutParams(AttributeSet) 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.

提交回复
热议问题