gpt4 book ai didi

android - v24 AsyncLayoutInflater 在 ui 线程上膨胀

转载 作者:行者123 更新时间:2023-11-30 01:13:19 25 4
gpt4 key购买 nike

“随着最近发布的 Android 支持库,第 24 版 Google 开发人员在 v4 库中加入了一个新的辅助类,用于布局的异步膨胀”

这是我的代码:

  Timber.e("Timeinflationstarts");
AsyncLayoutInflater asyncLayoutInflater=newAsyncLayoutInflater(this.getContext());
asyncLayoutInflater.inflate(R.layout.async_calendar,mLinearLayout,
new AsyncLayoutInflater.OnInflateFinishedListener() {
@Override
public void onInflateFinished(View view, int resid, ViewGroup parent) {

parent.addView(view);
Timber.e("timeWhenInflated");
}
});

这是输出:

 07-05 20:36:22.331 18250-18250/? E/PersonalFragment: Timeinflationstarts

07-05 20:36:22.371 18250-18250/? E/PersonalFragment: timeWhenInflated

问题是 async_calendar.xml 中的 View 在屏幕上显示时间更长(然后是 0.04 秒),阻塞了 UI 线程!

我使 View 变得更重,以确保我看到的延迟远远超过 0.04。

最佳答案

它可能由于多种原因而发生,

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.

此外,AsyncLayoutInflater 不能用于扩充包含 fragment 的布局。您可以在 docs 中了解更多信息

关于android - v24 AsyncLayoutInflater 在 ui 线程上膨胀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38209918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com