与AsyncTask的进度togther进度、AsyncTask、togther

2023-09-12 23:38:38 作者:温酒诉情深

我想用一个进度我的屏幕,而不是progressDialog上。

I want to use a progressbar on my screen instead of progressDialog.

我还插在我的XML视图文件中的进度,我希望把它显示在加载并禁用它时,没有加载。

I have inserted a progressBar on my XML-view file, and I want to make it show when it loads and disable it when not loading.

所以我用可见的,但它发生,所以数据的休息下来。

So I was using visible, but it takes place, so rest of the data comes down.

我应该如何在一个AsyncTask的使用进度?我怎样才能显示和隐藏呢?

How should I use progressbar in a asynctask? How can I show and hide it?

推荐答案

下面是一个最彻底的例子:

Here is a most exhaustive example:

public class ScreenSplash extends Activity {

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen_splash);

        final ProgressBar progress = (ProgressBar)findViewById(R.id.progress);
        final TextView    textview = (TextView)findViewById(R.id.text);

        new MyWorker(this, progress, textview).execute();
    }
}


final class MyWorker extends AsyncTask<Void, Integer, Void> {
    private static final int titles[] = {R.string.splash_load_timezone,
                                         R.string.splash_load_memory,
                                         R.string.splash_load_genres,
                                         R.string.splash_load_channels,
                                         R.string.splash_load_content};
    private static final int progr[]  = {30, 15, 20, 25, 20};

    private int index;

    private final Activity parent;
    private final ProgressBar progress;
    private final TextView textview;

    public MyWorker(final Activity parent, final ProgressBar progress, final TextView textview) {
        this.parent = parent;
        this.progress = progress;
        this.textview = textview;
    }

    @Override
    protected void onPreExecute() {
        int max = 0;
        for (final int p : progr) {
            max += p;
        }
        progress.setMax(max);
        index = 0;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected Void doInBackground(final Void... params) {
        /* Load timezone. this is very slow - may take up to 3 seconds. */
          ...
        publishProgress();

        /* Get more free memory. */
          ...
        publishProgress();

        /* Load channels map. */
          ...
        publishProgress();

        /* Load genre names. */
          ...
        publishProgress();


        /* Preload the 1st screen's content. */
          ...
        publishProgress();

        return null;
    }

    @Override
    protected void onProgressUpdate(final Integer... values) {
        textview.setText(titles[index]);
        progress.incrementProgressBy(progr[index]);
        ++index;
    }

    @Override
    protected void onPostExecute(final Void result) {
        parent.finish();
    }
}

有关shownig /隐藏进度条使用 progress.setVisibility(View.VISIBLE) progress.setVisibility(View.GONE)。还有 View.INVISIBLE 常数;从不同的 GONE 是你的进度栏不绘制,但仍占据其空间(有用的一些布局)。

For shownig/hiding your progress bar use progress.setVisibility(View.VISIBLE) and progress.setVisibility(View.GONE). There is also View.INVISIBLE constant; its difference from GONE is that your progress bar is not drawn but still occupies its space (usefull for some layouts).

 
精彩推荐
图片推荐