2011-07-31 34 views
3

使用setProgressDrawable代替ProgressBar使用自定義顏色對於我們而言無法正常工作。我們在ListView的行中使用進度條,但只有在列表中有多個元素時纔會顯示進度。在一個元素的情況下,進度條是空的。使用ProgressBar.setProgressDrawable問題

CursorAdapter.java:

public class CursorAdapter extends SimpleCursorAdapter { 
    public CursorAdapter(Context context, int layout, Cursor c, String[] from, 
      int[] to) { 
     super(context, layout, c, from, to); 
    } 

    public void bindView(View view, Context context, Cursor cursor) { 
     super.bindView(view, context, cursor); 
     updateProgressbar(view, cursor); 
    } 

    /** 
    * This method updates the progressbar using the "numberpages" and 
    * "currentpage" values. 
    */ 
    private void updateProgressbar(View view, Cursor cursor) { 
     ProgressBar progressBar = (ProgressBar) view 
       .findViewById(R.id.progressbarHorizontal); 

     progressBar.setProgressDrawable(view.getResources().getDrawable(
       R.drawable.greenprogress)); 

     progressBar.setMax(cursor.getInt(cursor.getColumnIndex("numberpages"))); 
     progressBar.setProgress(cursor.getInt(cursor 
       .getColumnIndex("currentpage"))); 
    } 

} 

/res/drawable/greenprogress.xml:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:id="@android:id/background"> 
    <shape> 
     <corners android:radius="5dip" /> 
     <gradient 
       android:startColor="#ff9d9e9d" 
       android:centerColor="#ff5a5d5a" 
       android:centerY="0.75" 
       android:endColor="#ff747674" 
       android:angle="270" 
     /> 
    </shape> 
</item> 

<item android:id="@android:id/secondaryProgress"> 
    <clip> 
     <shape> 
      <corners android:radius="5dip" /> 
      <gradient 
        android:startColor="#80ffd300" 
        android:centerColor="#80ffb600" 
        android:centerY="0.75" 
        android:endColor="#a0ffcb00" 
        android:angle="270" 
      /> 
     </shape> 
    </clip> 
</item> 

<item android:id="@android:id/progress"> 
    <clip> 
     <shape> 
      <corners 
       android:radius="5dip" /> 
      <gradient 
       android:startColor="#33FF33" 
       android:endColor="#008000" 
       android:angle="270" /> 
     </shape> 
    </clip> 
</item> 

</layer-list> 

有什麼不對的代碼和爲什麼它只要有一個以上的工作元素列表?沒有設置自定義的ProgressBar風格,一切都運行良好。 setProgressDrawable方法似乎會產生問題。

謝謝你幫助我們。

回答

5
private void updateProgressbar(View view, Cursor cursor) { 
     ProgressBar progressBar = (ProgressBar) view 
       .findViewById(R.id.progressbarHorizontal); 

     progressBar.setProgressDrawable(view.getResources().getDrawable(
       R.drawable.greenprogress)); 

> progressBar.setProgress(1); "Add this statement before setting the progress.." 

     progressBar.setMax(cursor.getInt(cursor.getColumnIndex("numberpages"))); 
     progressBar.setProgress(cursor.getInt(cursor 
       .getColumnIndex("currentpage"))); 
    } 
+0

謝謝你的回答。我們通過實施自己的進度條來解決問題。 – Dekar

+0

這解決了我的明顯錯誤。謝謝 –