更改标签是慢/ Laggy - 使用碎片碎片、标签、Laggy

2023-09-04 07:13:30 作者:万事不求人

我有一个使用标签的活动,并且标签切换片段。问题是,该片段需要几秒钟被创建时加载,从而切换标签具有约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 the doInBackground method. call publishProgress() from doInBackground() everytime you want to update the ui. Implement the UI updating logic in the onProgressUpdate method. Stop the progress bar in the onPostExecute method.

好运。