我试图在加载自定义 ListView 时显示进度条,然后隐藏它。我正在使用异步任务,但由于某种原因 - 未设置内容视图并且之前的布局视图被卡住,直到加载所有列表视图。
这是我的代码:
private ListView listViewGameResults;
protected View dialogLayout;
protected ArrayList<Game> listGames;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adresults);
GameResultsLoader gameResultsLoader = new GameResultsLoader();
gameResultsLoader.execute();
}
private class GameResultsLoader extends AsyncTask<Void, Void, Void> {
private GameResultsAdapter adapter;
public GameResultsLoader() {
}
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... params) {
try {
listGames = GameResultsCache.getInstance().getGameResults();
adapter = new GameResultsAdapter(getBaseContext(), listGames);
listViewGameResults = (ListView)findViewById(R.id.listViewGameResults);
}
catch (Exception e) {
// TODO Auto-generated catch block
finish();
}
return null;
}
@Override
protected void onPostExecute(Void res) {
listViewGameResults.setAdapter(adapter);
listViewGameResults.setDivider(null);
listViewGameResults.setDividerHeight(0);
ProgressBar pb = (ProgressBar)findViewById(R.id.progressbar_loading);
pb.setVisibility(View.GONE);
}
}
我的布局中的 ProgressBar 和 ListView:
<ProgressBar
style="?android:attr/progressBarStyle"
android:layout_centerInParent="true"
android:id="@+id/progressbar_loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/listViewGameResults"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="1dip"
android:layout_below="@+id/upperstrip"
android:layout_above="@+id/ivDownStrip" />