更改标签是慢/ Laggy - 使用碎片 [英] Changing Tabs is Slow/Laggy - Using Fragments
问题描述
我有一个使用标签的活动,并且标签切换片段。问题是,该片段需要几秒钟被创建时加载,从而切换标签具有约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.
的片段我的 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()
的逻辑,不更新UI 在doInBackground
方法。 - 呼叫
publishProgress()
从doInBackground()
每次要更新的用户界面。 - 实施中的
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.
好运。
这篇关于更改标签是慢/ Laggy - 使用碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!