Android : How to avoid header from scrolling in listview , android?

14,958

Solution 1

Instead of a header use the following layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <LinearLayout android:orientation="horizontal"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content">
      --> Your header layout here           
    </LinearLayout>

  <ListView android:id="@android:id/list"
      android:layout_width="fill_parent"
      android:layout_height="0dip"
      android:layout_weight="1" /> 
</LinearLayout>

Important things to notice:

  • The id of the ListView must be @android:id/list in order to be able to use a ListActivity
  • Use a 0 height and set the weight to 1

Solution 2

For me it worked fine when i included a tag called <include> before my listview, the code was something like this

<include layout="@layout/stationlist_header"/>
<ListView
            android:id="@+id/mylist"
            android:layout_width="match_parent"
            android:layout_height="210dp"
            android:layout_gravity="bottom"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
                        >
</ListView>
Share:
14,958
Srinivas
Author by

Srinivas

I am Srinivas , software engineer . Mobile applications Android , BlackBerry , Iphone.

Updated on June 17, 2022

Comments

  • Srinivas
    Srinivas almost 2 years

    I have a list view , where i am adding headerview to that list . every thing fine , but when am scrolling list headerview also moving with list, so i want to avoid headerview scrolling , i mean i have to scroll only list when i list reached to topview (titlebar), headerview has to remain bottom of titlebar .

    can any one gimme a solution for this ?

  • Srinivas
    Srinivas over 13 years
    for one header , its good idea. but if i had , differnt list patterns for same listview. i have given a headerview for each pattern
  • Srinivas
    Srinivas over 13 years
    for one header , its good one. but if i had , differnt list patterns for same listview. i have given a headerview for each pattern
  • eric2323223
    eric2323223 over 12 years
    One question is, how to align the header with its underline data?
  • Batdude
    Batdude about 10 years
    That works. But it seems very odd to me that the listview can have a header and footer that scroll away. Its hard to think of a use case where you would want them to scroll. I'd like to see a new attribute added to the ListView that locks the header and footer.