变高度列表视图

问题描述:

我需要编写一个变量高度的列表视图。我也需要它用于显示目的。列号是恒定的。我也需要不同颜色的一些细胞的背景。那么,如何实现这个?我应该去listview方式还是在scrollview上做一些自定义绘图? 一些片段可以帮助我。变高度列表视图

+0

你是什么意思“可变高度的ListView?”你想要单个物品的高度或整个列表的高度有所不同吗?如果你的意思是整个清单,应该确定它的高度? – adamp 2010-07-26 17:53:39

您的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; 
}