2010-07-26 77 views
1

我需要編寫一個變量高度的列表視圖。我也需要它用於顯示目的。列號是恆定的。我也需要不同顏色的一些細胞的背景。那麼,如何實現這個?我應該去listview方式還是在scrollview上做一些自定義繪圖? 一些片段可以幫助我。變高度列表視圖

+0

你是什麼意思「可變高度的ListView?」你想要單個物品的高度或整個列表的高度有所不同嗎?如果你的意思是整個清單,應該確定它的高度? – adamp 2010-07-26 17:53:39

回答

1

您的ListView

... 
    <ListView 
       android:id="@+id/android:list" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:scrollingCache="true" 
       android:divider="#000000" 
       android:dividerHeight="1px" 
       android:background="#FFFFFF" 
       android:cacheColorHint="#FFFFFF"     android:fastScrollEnabled="true"                     
       /> 
... 

你會膨脹diferent類型的 佈局,自定義大小和顏色裏面。 supossing我們有兩種類型,rowa.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="50px" 
    android:layout_gravity="center_horizontal" 
    android:background="#FF0000"> 
<TextView 
     android:id="@+id/row_section" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/imagearticle"  
     android:layout_toLeftOf="@id/Lrightaccessory"          
     android:capitalize="characters" 
     android:textStyle="bold"  
     android:ellipsize="marquee" 
     android:textColor="@color/colorSecciones" 
     android:focusable="false" 
     />          
</LinearLayout > 

和rowb.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="20px" 
    android:background="#00FF00"> 
<TextView 
     android:id="@+id/row_section" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/imagearticle"  
     android:layout_toLeftOf="@id/Lrightaccessory"          
     android:capitalize="characters" 
     android:textStyle="bold"  
     android:ellipsize="marquee" 
     android:textColor="@color/colorSecciones" 
     android:focusable="false" 
     />          
</LinearLayout > 

你決定在 至極位置虛增您在您的getview()函數不同的佈局。

public View getView(int position, View convertView, ViewGroup parent) {  
    View result=convertView;   

if (result == null) { 
    if (position == 0) { //Inflating row type A 
      LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      result = vi.inflate(R.layout.rowa, null); 
      views.set(position, result); 
      } 
    else if (position == 1) { //Inflating row type B     
      LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      result = vi.inflate(R.layout.rowb, null);     
     }       
    } 
    return result; 
}