如何在加载数据时在 AsyncTask 上实现旋转进度条



假设我有以下代码:

.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 在后台工作时隐藏回收器视图?抱歉,如果这看起来像一个简单的问题,但我找到的唯一答案是 ProgressDialog,该对话框从 API 26 开始已弃用。提前谢谢你。

重写预执行方法并显示进度对话框。

您可以在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
    }
}

最新更新