这是我一直在使用的东西。我不谈论您的 UI、您的列表框更新等。这只是 Task 和 ProgressDialog 处理:
ProgressDialog 的样式:
<resources>
<style name="MyProgressDialog" parent="@android:style/Theme.Dialog">
<item name="android:background">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowTitleStyle">@null</item>
</style>
</resources>
我的 ProgressDialog 类:
public class MyProgressDialog extends Dialog {
public MyProgressDialog(Context context) {
super(context, R.style.MyProgressDialog);
}
public static MyProgressDialog show(Context context, CharSequence title, CharSequence message) {
return show(context, title, message, false);
}
public static MyProgressDialog show(Context context, CharSequence title, CharSequence message, boolean indeterminate) {
return show(context, title, message, indeterminate, false, null);
}
public static MyProgressDialog show(Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable) {
return show(context, title, message, indeterminate, cancelable, null);
}
public static MyProgressDialog show(Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable, OnCancelListener onCancelListener) {
MyProgressDialog dialog = new MyProgressDialog(context);
dialog.setCancelable(cancelable);
dialog.setOnCancelListener(onCancelListener);
dialog.setTitle(title);
dialog.addContentView(new ProgressBar(context), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
dialog.show();
return dialog;
}
}
现在是牛肉。以 AsyncTask 作为内部类的 ListView。AsyncTask 持有对周围 ListActivity(上下文)的引用,而 ListActivity 持有对 AsyncTask 的引用。ProgressDialog 将在通常启动时在 AsyncTask 中创建。如果 ListActivity 停止并且 AsyncTask 仍在运行,则对话框将从 ListActivity 中消失并在重新启动后重新创建。
重要的是要注意谁在做什么:
public class MyListActivity extends ListActivity {
private class MyAsyncTask extends AsyncTask<Void, Integer, Boolean> {
/* package */ MyListActivity context;
/* package */ MyProgressDialog dialog;
public MyAsyncTask(MyListActivity context) {
super();
this.context = context;
}
@Override
protected Boolean doInBackground(final Void... voids) {
Boolean rc = false;
// Put your running task here
return rc;
}
@Override
protected void onPostExecute(final Boolean result) {
if (dialog != null) {
try {
dialog.dismiss();
} catch (Exception exception) {
}
dialog = null;
}
if (result) {
// Whatever you need to update in UI
}
context.task = null;
}
@Override
protected void onPreExecute () {
dialog = MyProgressDialog.show(context, null, null, true, false);
}
}
/* package */ MyAsyncTask task;
// Save AsyncTask if running, dismiss ProgressDialog if open
@Override
public Object onRetainNonConfigurationInstance() {
if (task != null) {
if (task.dialog != null) {
task.dialog.dismiss();
task.dialog = null;
}
}
return task;
}
@Override
public void onCreate(final Bundle bundle) {
// Usual start here
// Was task running?
task = (MyAsyncTask) getLastNonConfigurationInstance();
if (task != null) {
// Task was running, update context, restart ProgressDialog
task.context = this;
task.dialog = MyProgressDialog.show(this, null, null, true, false);
} else {
// Task was not running. Start from beginning
task = new MyAsyncTask(this);
task.execute();
}
}