![](/img/trans.png)
[英]How do I make a android AsyncTask loading spinner while doInBackgroung()
[英]How do I implement a spinning ProgressBar on a AsyncTask while the data is loading?
假設我有以下代碼:
XML格式
<FrameLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<android.support.v7.widget.RecyclerView
android:id="@+id/photo_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
程序
private class FetchItemsTask extends AsyncTask<Void, Void, List<GalleryItem>> {
private String mQuery;
public FetchItemsTask(String query) {
mQuery = query;
}
@Override
protected List<GalleryItem> doInBackground(Void... params) {
if (mQuery == null) {
return new FlickrFetchr().fetchRecentPhotos();
} else {
return new FlickrFetchr().searchPhotos(mQuery);
}
}
@Override
protected void onPostExecute(List<GalleryItem> galleryItems) {
mItems = galleryItems;
setupAdapter();
}
}
當AsycnTask在后台運行時,如何實現旋轉的ProgressBar並隱藏RecyclerView? 抱歉,這似乎是一個簡單的問題,但是我發現的唯一答案是使用ProgressDialog,該方法自API 26起已棄用。謝謝。
覆蓋預執行方法並顯示進度對話框。
您可以在onPreExecute()
顯示微調onPreExecute()
,並在onPostExecute()
其隱藏。
class Your_Class extends AsyncTask<...>
{
ProgressBar pb;
Your_Class(){
// initialize pb
}
Protected void onPreExecute(){
// show progressbar
}
protected void onPostExecute(Bitmap image){
// hide progressbar
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.