Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView with fixed header and footer

How can I create a ListView with fixed header and footer?

I don't want the header/footer to scroll with the items in the ListView.

Is it possible that the header/footer is floating over the ListView so that the header/footer dont need to have a straight bottom/top background and the ListView items scrolls below the background of the header/footer view, but still show the first element of the list?

like image 944
Goran Avatar asked May 29 '13 11:05

Goran


2 Answers

I solved it by using @blackbelt suggestion and a small ImageView with the source image being transparant with a tile background.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >

<ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
  android:layout_above="@+id/tv_footer"
android:layout_below="@+id/tv_header" />

<TextView
android:id="@+id/tv_footer"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/footer_bg"
android:gravity="center"
android:text="Footer" />

<TextView
android:id="@+id/tv_header"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/header_bg"
android:gravity="center"
android:orientation="vertical"
android:text="Header" />

<ImageView
android:id="@+id/iconView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/lv"
android:background="@drawable/header_bg2"
android:src="@drawable/transparant_bg_tile" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/tv_footer"
android:layout_alignParentRight="true"
android:background="@drawable/footer_bg2"
android:src="@drawable/transparant_bg_tile" />

</RelativeLayout>

Screenshot from device enter image description here

like image 78
Goran Avatar answered Nov 11 '22 20:11

Goran


http://developer.android.com/reference/android/widget/ListView.html#addHeaderView%28android.view.View%29. Check this for addHeaderView(param).

http://developer.android.com/reference/android/widget/ListView.html#addFooterView%28android.view.View%29. Check this for addFooterView(param).

Example of the method usuage by inlfating a layout @ Android listview with header and footer buttons

You can use addHeaderView and addFooterView for the list to add header and footer.

You can do as what @blackbelt suggested. I have used a relative layout instead of LinearLayout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >

<ListView
    android:id="@+id/lv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
      android:layout_above="@+id/textView1"
    android:layout_below="@+id/tv1" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:gravity="center"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    android:text="Footer" />

<TextView
    android:id="@+id/tv1"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:gravity="center"
    android:layout_alignParentTop="true"
    android:orientation="vertical"
    android:layout_centerHorizontal="true"
    android:text="Header" />

</RelativeLayout>

Graphical Layout snap shot

enter image description here

like image 11
Raghunandan Avatar answered Nov 11 '22 21:11

Raghunandan