我想在我的屏幕上使用进度条而不是progressDialog。进度条与asyncTask
我已经在我的XML-view文件中插入了一个progressBar,并且我想在加载时显示它,并在不加载时将其禁用。
所以我使用可见,但它发生,所以其余的数据下降。
我应该如何在asynctask中使用进度条?我如何显示和隐藏它?
我想在我的屏幕上使用进度条而不是progressDialog。进度条与asyncTask
我已经在我的XML-view文件中插入了一个progressBar,并且我想在加载时显示它,并在不加载时将其禁用。
所以我使用可见,但它发生,所以其余的数据下降。
我应该如何在asynctask中使用进度条?我如何显示和隐藏它?
至于使用来自异步任务的进度条,您可以使用doInBackground(int Progress)
中的PostPrgressUpdate()
和OnProgressUpdate()
方法更新ProgressBar。 至于酒吧的显示和隐藏我无法理解你的问题(或问题,请修改)
这里是一个最详尽的例子:
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
的区别在于你的进度条并未绘制,但仍占用其空间(对某些布局有用)。
与这个领域的很多例子一样,它没有考虑在任务正在运行时活动被破坏(可能重新创建)的可能性... – 2010-11-07 22:41:31
是, 确实如此。有关于这个问题的好帖子:http://stackoverflow.com/questions/3357477/is-asynctask-really-massively-flawed-or-am-i-just-missing-something – JBM 2010-11-08 08:57:46
事实上,一个必须杀死他们在onDestroy中的bg任务。活动指南指出,在活动被破坏后,应该没有任何东西在后台运行(否则你会排出手机的电池)。因此,你应该强制完成你的AsyncTasks,即使它只是用户翻转手机,并开始全部在onCreate - 愚蠢的,是的,但是,如果谷歌已经把这个Activity的生命周期放在这里,我们该怎么办? – JBM 2010-11-08 09:15:24
<ProgressBar
android:id="@+id/progress_bar"
style="@android:style/Widget.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
然后在Asyntask
ProgressBar mProgress=(ProgressBar) findViewById(R.id.progress_bar);
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
mProgress.setVisibility(View.GONE);
}
[从更新的AsyncTask进度对话框在活动]的可能重复(http://stackoverflow.com/questions/4591878/updating- progress-dialog-in-activity-from-asynctask) – 2013-01-23 13:14:39