2011-03-10 62 views
19

我想在加載數據時在我的應用程序中顯示一個進度環。我有一個活動,並從一個活動移動到另一個我解析一些XML數據,所以暫時解析完成後,我想顯示一個循環加載效果。我想要一個進度圈而不是進度對話框

+2

這是我做到了http://stackoverflow.com/questions/19655715/progress-dialog-is-closed-when-touch-on-screen/ 22939109#22939109 – Ali 2014-04-29 09:45:15

回答

3

爲什麼當加載數據和stopAnimation不使用進度UI

myProgressDialog = ProgressDialog.show(ListingPage.this,"Please Wait", "Loading Date", true); 

+0

我不能使用這個progressDialog因爲我的客戶不想要這個,他想要循環加載效果。 – ta54 2011-03-10 12:43:35

+0

*** ProgressDialog現已棄用。 – Digitalsa1nt 2017-11-02 13:51:59

17

您需要創建在res /動畫文件夾的動畫XML文件,並調用startAnimation在您的ImageView當你停止加載數據。並設置加載圖像的ImageView,例如:

loading http://bigod26.com/pics/loading.png

這對圈動畫xml文件的ID碼

<?xml version="1.0" encoding="UTF-8"?> 
<rotate 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromDegrees="0" 
    android:toDegrees="360" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:repeatCount="infinite" 
    android:duration="1200" 
    android:interpolator="@android:anim/linear_interpolator" /> 
+0

我正在使用自定義適配器的列表視圖。我想在該列表視圖中顯示加載效果。我怎麼能得到解析完成,我們可以停止這個動畫 – ta54 2011-03-10 13:36:31

+0

我也使用列表視圖。而當另一個線程解析xml時,我設置爲丟失視圖特殊的loadAdapter,只返回一個視圖。這個視圖是imageView,它將所有的父母和適合的圖像填充到中心。當解析線程停止解析我設置爲免費查看我提供UI元素的另一個自定義適配器。 – RomaTTi 2011-03-10 13:47:26

+0

你能給我一個例子使用它。謝謝! – 2016-03-16 04:03:55

1

正如你的進度對話框,使用動畫與替代異步任務只需在預執行時將其顯示出來並在帖子上再次隱藏即可。

2

使用的AsyncTask加載和解析:

/** 
    * Background task that fetched the content from server and parses the content. 
    */ 
    private class BackgroundLoadingTask extends AsyncTask<InputStream, Void, Boolean> { 

    @Override 
    protected void onPreExecute() { 
     // show the progress bar 
     Activity.this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
     Activity.this.requestWindowFeature(Window.PROGRESS_VISIBILITY_ON); 
    } 

    @Override 
    protected Boolean doInBackground(InputStream... params) { 

     // try to load XML from HTTP server and parse 

     return true; 
    } 


    @Override 
    protected void onPostExecute(Boolean parsingError) { 
     // hide the progress bar 
     Activity.this.requestWindowFeature(Window.PROGRESS_VISIBILITY_OFF); 


    } 
    } 
+1

請注意,如果您已全屏或隱藏標題欄,並非所有這些功能都能正常工作。 – trgraglia 2011-03-10 14:09:00

+0

'android.util.AndroidRuntimeException:在添加內容之前必須調用requestFeature() – dentex 2013-09-16 13:10:07

28

您可以使用一個不確定的進度爲循環負載效應。這裏是你如何在XML中做到這一點:

<ProgressBar android:indeterminate="true" 
      android:layout_width="50dp" android:layout_height="50dp" 
      android:id="@+id/marker_progress" style="?android:attr/progressBarStyle" 
      android:layout_gravity="center_vertical|center_horizontal"/> 

你可以改變高度和寬度是你想要的。當你完成加載後,你可以改變它對View.INVISIBLE或View.GONE的可見性

1

要擴大trgraglia的答案,你可以創建一個不確定的(行爲:循環)進度條並將可見性設置爲false。在AsyncTask preExecute()期間,使其可見,並且在AsyncTask onPostExecute()期間,將其恢復爲不可見(假設您保留在相同的活動或需要的情況下)。這消除了創建動畫的需要。

4

插入此beetween兩個View標籤

<ProgressBar android:id="@+id/loading_spinner" 
     style="?android:progressBarStyleLarge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" />