我想在加載數據的同時在活動中顯示進度條,並且在加載完成時隱藏進度條(GONE)! 並嘗試此代碼:(點擊鏈接加載數據)如何在加載數據並填入recyclerview時顯示進度條?
private void allMoment() {
BTN_ALL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
TBL_MOMENT = new TableMoment(DB_HELPER);
// MOMENTS = TBL_MOMENT.MOMENT_LIST();
new loadTask().execute();
filterResult();
EDT_SEARCH.setText("");
} catch (Exception e) {
displaySnackBar(v, "خطا:" + "\n" + e.toString());
}
}
});
}
和工作方法:
private class loadTask extends AsyncTask<String, Integer, Boolean> {
@Override
protected void onPreExecute() {
PROGRESS_LOADER.setVisibility(View.VISIBLE);
super.onPreExecute();
}
@Override
protected void onPostExecute(Boolean result) {
PROGRESS_LOADER.setVisibility(View.GONE);
adapter.notifyDataSetChanged();
super.onPostExecute(result);
}
@Override
protected Boolean doInBackground(String... params) {
MOMENTS = TBL_MOMENT.MOMENT_LIST();
try {
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
與dapter填寫回收:
private void filterResult() {
try {
adapter = new HistoryAdapter(this, MOMENTS);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
} catch (Exception e) {
displayToast(this, "خطای آداپتر:" + "\n" + e.toString());
}
}
但數據成爲負擔,並填寫進入recyclerview並不顯示進度條 如何解決這個問題?
「This filterResult();耗時?」是的...我希望數據加載時,用戶看到進度條 –
if filterResult();阻止UI線程ProgressBar將不會顯示或更新,您應該在分離的線程中運行filterResult()。 –