mynameisanthpny
mynameisanthpny

Reputation: 689

variable height list view

i need to write a variable height listview. also i need it for display purpose. column numbers are constant. also i need backgrouund of some cells in different color. so, how to achive this ? should i go listview way or do some custom drawing on a scrollview ? some snippets would help me.

Upvotes: 1

Views: 1294

Answers (1)

Jorgesys
Jorgesys

Reputation: 126455

inside your 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"                                                                                   
                />  
...

you will inflate diferent types of layouts, customizing sizes and colours. supossing we have two types, 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 >

and 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 >

And the in your getview() function you decide in wich position inflate your different layouts.

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

Upvotes: 1

Related Questions