【发布时间】:2016-06-11 16:44:27
【问题描述】:
我想在后台生成表格时显示一个圆形进度条。我在异步任务中的代码:
class DoWork extends AsyncTask<Void, Void, Void>{
Context context;
public DoWork(Context con){
this.context = con;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
setContentView(R.layout.content_progress);
pb = (ProgressBar) findViewById(R.id.progressBar);
pb.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... params) {
runOnUiThread(new Runnable() {
public void run() {
setContentView(new TableMainLayout(context));
}
});
return null;
}
@Override
protected void onPostExecute(Void result) {
pb.setVisibility(View.INVISIBLE);
super.onPostExecute(result);
}
}
content_progress.xml 中的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.standart.trainschedule.ProgressActivity"
tools:showIn="@layout/activity_progress">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
new TableMainLayout(context) - 使用数据生成表格布局,它也是一个 UI,这就是我在 doInBackground() 中使用 runOnUiThread() 的原因
问题是当我在手机上运行我的应用程序时,它没有显示进度条。它只是冻结了我的应用程序 5 秒。然后给我看一个生成的表格。
我应该怎么做才能显示进度条?
【问题讨论】:
-
从不在 AsyncTask 中执行
runOnUiThread。它破坏了 AsyncTask 的全部目的 -
你的布局是从互联网或类似的地方获取数据吗?
-
nasch,TableMainLayout 从数据库中获取数据。
标签: android android-asynctask android-progressbar