0

有,不確定progressBar,它旋轉,直到ListView內容準備好顯示。 progressBarlistView都是相同的Fragment的一部分。如何控制不屬於ListView的不確定的ProgressBar?

所以我編碼,這progressBar是取得內容的可見和去掉AsyncTask

mProgressBar = (ProgressBar) view.findViewById(R.id.progressbar); 
mProgressBar.getIndeterminateDrawable() 
      .setColorFilter(getResources().getColor(R.color.pink), PorterDuff.Mode.MULTIPLY); 

new MyAsyncTask().execute(); 

//then in asynctask 
private class MyAsyncTask extends AsyncTask<Void, Integer, MyAdapter> { 
    @Override 
    protected void onPreExecute() { 
     mProgressBar.setVisibility(View.VISIBLE); 
    } 

    @Override 
    protected MyAdapter doInBackground(Void... params) { 
     //do the stuff and return loaded adapter 

     return adapter; 
    } 

    @Override 
    protected void onPostExecute(MyAdapter adapter) { 
     mProgressBar.setVisibility(View.GONE); 
    } 
} 

然而progressBar被凍結,不轉?!

我嘗試從onPreExecute實例化progressBar,但外部progressBar不接受AsyncTask中設置的參數。我可以看到,因爲它以默認顏色顯示,而不是自定義。

那麼如何控制這個progressBar?現在我需要的是在內容開始加載時顯示它,並在準備顯示時將其隱藏起來。

注意:我不需要在每個項目內progressBar,但我需要它的方式我描述。

回答

0

您是否嘗試過爲MyAsyncTask創建構造函數,並將其作爲參數提供給progressBar?

您可以像在任何其他Java類中一樣保存私有屬性。

編輯:我會定義一個自定義處理程序,並從活動中調用它,與progressBar的可見性一起玩。

public final class LoadingDialogHandler extends Handler { 
    private final WeakReference<Activity> mActivity; 
    // Constants for Hiding/Showing Loading dialog 
    public static final int HIDE_LOADING_DIALOG = 0; 
    public static final int SHOW_LOADING_DIALOG = 1; 

    private View mLoadingDialogContainer; 

    public LoadingDialogHandler(Activity activity, View container) { 
     mActivity = new WeakReference<Activity>(activity); 
     mLoadingDialogContainer = container; 
    } 

    public void handleMessage(Message msg) { 
     Activity activity = mActivity.get(); 
     if (activity == null) { 
      return; 
     } 
     if (msg.what == SHOW_LOADING_DIALOG) { 
      mLoadingDialogContainer.setVisibility(View.VISIBLE); 

     } else if (msg.what == HIDE_LOADING_DIALOG) { 
      mLoadingDialogContainer.setVisibility(View.GONE); 
     } 
    } 

}

,並調用它是這樣的:

在你的活動/片段申報處理的副進度條:

LoadingDialogHandler loadingDialogHandler = new LoadingDialogHandler(yourActivity, findViewById(R.id.loading_indicator)); 

顯示/隱藏(樣本顯示):

loadingDialogHandler.sendEmptyMessage(LoadingDialogHandler.SHOW); 

PS:也許你在UIthread像這樣運行:

activity.runOnUiThread(new Runnable() { 
       public void run() { 
        //update progressBar 
       } 
      }); 
+0

我做到了。但'ProgressBar'仍然不旋轉。看起來像冰凍。沒有更新進度條。這是不確定的進度條。 – sandalone 2014-10-04 10:38:53

+0

我已經編輯了我的答案,我希望這可以幫助你現在;) – 2014-10-04 12:01:51

+0

好主意,我將在另一個項目中使用,但ProgressBAr仍然凍結。 – sandalone 2014-10-07 11:42:20

0

這就是它是如此簡單,有針對功能的內置API:

mProgressBar = (ProgressBar) view.findViewById(R.id.progressbar); 
listView.setEmptyView(mProgressBar); 

完成!當列表爲空時,listView將自動調用setVisibility和GONE和VISIBLE

+0

當適配器正在加載時,這不起作用。只有在適配器爲空或爲空的情況下。 – sandalone 2014-10-07 11:35:30

+0

但是,當它加載時,適配器是空的,您也可以在適配器完成加載後將其設置爲列表。 – Budius 2014-10-07 12:20:23

相關問題