2015-03-03 30 views
1

如何在Android應用程序中將ProgressBar添加到GridView的每個項目?我需要在GridView元素的右上角有進度條。每個GridView元素的進度條

+0

使用自定義gridItems製作GridView。就如此容易。請參閱[示例](http://www.technotalkative.com/android-gridview-example/)瞭解如何製作自定義GridView。只需用ProgressBar替換ImageView並將其放在任何你想要的地方。 :) – 2015-03-03 12:29:54

+0

好的,我會在晚上檢查出來:)如果我有任何問題,我會問在這裏,謝謝你! – dammarpol 2015-03-03 12:32:58

+0

亮點類 – tomrozb 2015-03-06 10:41:05

回答

1

您需要創建一個自定義適配器,以擴大包含ProgressBar的視圖。然後在運行期間,您需要更新ProgressBar的進度。這裏有一些非常基本的例子可以幫助你入門。

row_grid_view只是一個包含進度條的佈局。你將不得不使用可用的佈局來查看哪些符合你的需求。一個友好的警告:如果佈局是適配器的一部分,不要使用RelativeLayout。如果你不知道你在做什麼,它們可能會非常昂貴:)

ProgressBarAdapter是一個適配器,它顯示了一個ProgressItem s的列表。這些項目僅包含自己的進度,以便可用於更新每個ProgressBar的進度。

row_grid_view.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <ProgressBar 
     android:id="@+id/progress" 
     android:layout_width="wrap_content" 
     android:max="100" 
     android:layout_height="wrap_content" 
     style="@style/Widget.AppCompat.ProgressBar.Horizontal" /> 

</FrameLayout> 

ProgressBarAdapter

public class ProgressShowingAdapter extends BaseAdapter { 


    private ArrayList<ProgressItem> mData; 
    private LayoutInflater mInflater; 

    public ProgressShowingAdapter(Context context) { 
     this.mInflater = LayoutInflater.from(context); 
    } 

    @Override 
    public int getCount() { 
     return mData.size(); 
    } 

    @Override 
    public ProgressItem getItem(int position) { 
     return mData.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     // if your items have any unique ids, return that instead 
     return position; 
    } 

    public void setData(List<ProgressItem> newData) { 
     this.mData.clear(); 
     if (newData != null && !newData.isEmpty()) { 
      mData.addAll(newData); 
     } 
    } 

    private static class ViewHolder { 
     private ProgressBar mProgress; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // view holder pattern 
     ViewHolder vh = null; 
     if (convertView == null) { 
      vh = new ViewHolder(); 
      convertView = mInflater.inflate(R.layout.row_grid_view, parent, false); 
      vh.mProgress = (ProgressBar) convertView.findViewById(R.id.progress); 
      convertView.setTag(vh); 
     } else { 
      vh = (ViewHolder) convertView.getTag(); 
     } 
     ProgressItem mItem = getItem(position); 
     vh.mProgress.setProgress(mItem.getProgress()); 

     // do the remaining of the stuff here 
     return convertView; 
    } 
} 

ProgressItem

public class ProgressItem { 
    private int mProgress; 

    public ProgressItem(int mProgress) { 
     this.mProgress = mProgress; 
    } 

    public int getProgress() { 

     return mProgress; 
    } 
} 
+0

yess please :)會很棒! – dammarpol 2015-03-03 12:56:48

+0

謝謝,它幫助我:)我標記你的答案是最後一個 – dammarpol 2015-03-06 14:24:22