2012-09-18 68 views
2

在我的應用程序中,我有一個ListView,ListView中的每個項目都有一個GridLayout。 我通過適配器類向GridLayout添加一個自定義視圖。添加到GridLayout的視圖的大小不一樣。它可能因視圖而異。 我在getView()適配器代碼,Android:GridLayout的分隔線

for (int i = 0,j=0; i < (data.get(position).getItems().size() * 2) && j< (data.get(position).getItems().size()); i=i+2,j++) { 
      View childView=getChildView(position, j); 
      childView.setBackgroundColor(Color.CYAN); 
      Log.i("ChildView Height",childView.getLayoutParams().height+" length"); 
      holder.order_row.addView(childView, i); 
      holder.order_row.addView(getItemSeparatorView(minHeight),i+1); 
     } 

private View getChildView(int pos, int i) { 

     //Log.i("position in child", pos + ""); 

     View child = inflater.inflate(R.layout.order_item, null); 
     InnerViewHolder inholder = new InnerViewHolder(); 
     inholder.name = (TextView) child.findViewById(R.id.order_name); 
     inholder.items = (ListView) child.findViewById(R.id.order_list); 

     inholder.name.setText(data.get(pos).getItems().get(i).getItemName()+":"); 
     inholder.name.setTextColor(itemColor); 
     inholder.items.setAdapter(new OrderItemOptionAdapter(context, data 
       .get(pos).getItems().get(i).getOptions(),itemColor)); 
     int size = data.get(pos).getItems().get(i).getOptions().size(); 
     Log.i("childview", "called" + i+ "size is "+size); 
     if (size > 7) { 
      child.setLayoutParams(new GridLayout.LayoutParams(new LayoutParams(
        260, 80 + (size * 20)))); 

     } else if(size <=7 && size>=4){ 
      child.setLayoutParams(new GridLayout.LayoutParams(new LayoutParams(
        260, 450))); 
     } else { 
      child.setLayoutParams(new GridLayout.LayoutParams(new LayoutParams(
        260, 250))); 
     } 

     return child; 
    } 
    public View getItemSeparatorView(int h){ 
     //Log.i("seperator","called and height is "+h); 
     View sep = inflater.inflate(R.layout.item_seperator, null); 
     sep.setLayoutParams(new GridLayout.LayoutParams(new LayoutParams(2,h))); 
     return sep; 
    } 

和我的XML代碼。

<GridLayout 
     android:id="@+id/order_row" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:columnCount="6" 
     android:orientation="horizontal" 
> </GridLayout> 

我想添加一個分隔線,它具有childViews的最大高度。 而且在某些情況下,網格佈局可能有多於一行,因此分隔視圖的高度應根據GridLayout一行中視圖的最大高度進行更改。

如果我可以首先添加所有的子視圖,像索引0,2,4等。然後計算Childviews的最大高度。在計算最大高度後,我可以添加索引分隔視圖像1,3,5,7etc ..

請爲我提供正確的方法來做到這一點。 謝謝。

+0

minHeight = 260;我爲固定尺寸的Android設備做這個應用程序。 – Sridhar

+0

getChildView()用於在GridLayout中添加視圖 – Sridhar

+0

getItemSeparatorView(int)用於兩個子視圖之間的分隔線視圖 – Sridhar

回答

-6

集Verticalspacing和網格視圖XML文件水平間距

<GridView 
     android:horizontalSpacing="10dp" 
     android:horizontalSpacing="10dp" > 

    </GridView> 

我希望這會幫助你。