更改标签很慢/滞后 - 使用片段 [英] Changing Tabs is Slow/Laggy - Using Fragments
问题描述
我有一个使用标签的活动,标签切换片段.问题是 Fragment 在创建时需要几秒钟才能加载,因此切换选项卡有大约 1 或 2 秒的延迟.为了解决这个问题,我一直在尝试找到一种方法来显示一个简单的加载图形甚至是一个进度对话框,以便选项卡立即更改并显示一些指示正在加载的内容,直到一切完成.
I have an Activity that uses tabs, and the tabs switch Fragments. The problem is that the Fragment take a few seconds to load when being created, thus switching tabs has a delay of about 1 or 2 seconds. To fix this I have been trying to find a way to display a simple Loading graphic or even a progress dialog, so that the tab changes instantly and displays something indicating things are loading until everything completes.
我的 Fragment 的 onCreateView
方法如下所示:
My onCreateView
method of the Fragment looks like this:
FrameLayout fl;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
fl = (FrameLayout) inflater.inflate(R.layout.text_layout, container, false);
doHeavyStuff();
return fl;
}
我尝试将 doHeavyStuff()
放在 onStart()
中,但这没有任何帮助.线程也无济于事,因为 doHeavyStuff()
涉及操作视图/GUI.
I tried putting doHeavyStuff()
in onStart()
but that did not help anything. And a Thread won't help because doHeavyStuff()
involves manipulating views/GUI.
关于如何显示片段并显示正在加载"信息其他所有内容加载的任何想法?
Any ideas on how I can display the Fragment and display "Loading" information while everything else loads?
谢谢!
马特.
推荐答案
在onActivityCreated()
中:
In the onActivityCreated()
:
- 启动一个
进度条
(或任何显示UI元素的进度). - 执行下面解释的
HeavyStuffDoingAsyncTask
.
- Start a
progress bar
(or any progress showing UI element/s). - Execute the
HeavyStuffDoingAsyncTask
which is explained below.
HeavyStuffDoingAsyncTask 应该有:
doHeayStuff()
的逻辑,在doInBackground
方法中不更新 ui.- 每次要更新 ui 时,从
doInBackground()
调用publishProgress()
. - 在
onProgressUpdate
方法中实现UI更新逻辑. - 在
onPostExecute
方法中停止进度条.
doHeayStuff()
's logic which doesn't update ui in thedoInBackground
method.- call
publishProgress()
fromdoInBackground()
everytime you want to update the ui. - Implement the UI updating logic in the
onProgressUpdate
method. - Stop the progress bar in the
onPostExecute
method.
祝你好运.
这篇关于更改标签很慢/滞后 - 使用片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!