0

我在RelativeLayout中有一個列表視圖,它從AsyncTask自動加載內容。現在我想在列表視圖的頂部添加一個水平進度條,以便在元素加載時顯示進度(不在疊加層上)。我嘗試了一些方法,但進度條沒有正確顯示。什麼是正確的做法?在RelativeLayout中顯示列表視圖的進度條

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 
    <ProgressBar 
    android:id="@+id/progressBar1" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/listViewTitle" 
    android:layout_alignParentTop="true" /> 
<ListView 
    android:id="@+id/listViewTitle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" > 
</ListView> 

回答

0

在代碼中,你可以這樣做:

public static final int DIALOG_DOWNLOAD_PROGRESS = 0; 
private ProgressDialog mProgressDialog; 
---- 
---- 

    @Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case DIALOG_DOWNLOAD_PROGRESS: 
      mProgressDialog = new ProgressDialog(this); 
      mProgressDialog.setMessage("Downloading data file.."); 
      mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      mProgressDialog.setCancelable(false); 
      mProgressDialog.show(); 
      return mProgressDialog; 
     default: 
      return null; 
     } 
    } 

---- 
---- 
In AsyncTask: 


    @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      showDialog(DIALOG_DOWNLOAD_PROGRESS); 
     } 

     @Override 
     protected String doInBackground(String... urls) { 

      ---- 
      ---- 
      publishProgress("" + progress); 
     } 

     protected void onProgressUpdate(String... progress) { 
     mProgressDialog.setProgress(Integer.parseInt(progress[0])); 
    } 

     @Override 
     protected void onPostExecute(String result) { 
      dismissDialog(DIALOG_DOWNLOAD_PROGRESS); 
     } 
+0

該問題指定:「...不在疊加層上」。然後你的建議(一個對話框)將不起作用。你能更新你的答案嗎? – Vikram

0

無論是ProgressBarListView對齊到頂部。在這種情況下,ListView將重疊ProgressBar。嘗試對齊ListViewProgressBar如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:paddingBottom="@dimen/activity_vertical_margin" 
       android:paddingLeft="@dimen/activity_horizontal_margin" 
       android:paddingRight="@dimen/activity_horizontal_margin" 
       android:paddingTop="@dimen/activity_vertical_margin" 
       tools:context=".MainActivity" > 

    <ProgressBar android:id="@+id/progressBar1" 
       style="?android:attr/progressBarStyleHorizontal" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignLeft="@+id/listViewTitle" 
       android:layout_alignParentTop="true" /> 

    <ListView android:id="@+id/listViewTitle" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentTop="true" 
       android:layout_below="@id/progressBar1" /> 

</RelativeLayout> 

對於你的情況,你可能會更好使用LinearLayout代替RelativeLayout

2

使用LinearLayout並在開始或完成加載時切換可見性ProgressBar

目前,您在RelativeLayout的頂部邊緣都有兩個視圖。