我的 Activity 在 linearLayout(R.id.liste,在 scrollView 内)中有一些 fragment ,需要一些时间才能显示。 我将数组的大小减少到 50,但它是第一个 350。 每个 fragment 都是独立的,因为我需要它们响应点击并弹出对话框。
代码如下:
long start= System.currentTimeMillis();
for (Item item : dao.getList(triKey, start, end)) {
ItemFragment fragment = ItemFragment.newInstance(item);
FragmentTransaction fragTransaction = fragMan.beginTransaction();
fragTransaction.add(R.id.liste, fragment);
fragTransaction.commit();
}
long time = System.currentTimeMillis()-start;
真题:
我想显示一个圆形 progressBar 并在显示所有 fragment 时将其设置为 Gone,但 Activity 没有 onViewCreated() 方法,是否有任何解决方法?
更深层次的理论问题:
如果我覆盖 Activity.onCreateView() 并使用 System.currentTimeMillis(),我发现在我的 Nexus 4 上显示 50 个 fragment 需要 7.5 秒(350 个 fragment 需要 20 秒)。 由于每个 fragment 仅包含 5 个 TextView 和 1 个 ProgressBar 并且需要大约 10-15 毫秒来创建其 View ,我很惊讶它花了这么长时间,为什么添加 fragment 比绘制它花费的时间多近 6 倍?
我不知道您需要回答哪一段代码,当然可以随时提问!
非常感谢您的帮助!
最佳答案
but Activity doesn't have a onViewCreated() method, is there any workaround ?
没有。
am I doing something wrong ?
是的。
您确实需要重新考虑您的设计。您可以通过在 Activity
(不使用 ScrollView
)中使用 ListView
或使用 ListView 轻松完成您所描述的操作
在单个 ListFragment
中。
ListView
在滚动时重复使用其列表项 View - 如果您使用自定义 Adapter
,它将按照您的意愿运行并且非常高效。您可能还想查看“ View 持有者”模式,以提高滚动效率。
关于安卓 : Activity onviewcreated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25503890/