Android ListView with fixed header and footer
Asked Answered
P

7

13

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?

Puss answered 29/5, 2013 at 11:4 Comment(0)
P
16

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

Puss answered 29/5, 2013 at 12:59 Comment(0)
U
10

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

Unemployment answered 29/5, 2013 at 11:27 Comment(2)
That works fine. I used this solution but added a transparant ImageView with a tile background for the sawtooth effect below and above the header/footer.Puss
@Puss pls mark the answer by clicking on the tick once if it helps. It will heps others who visit the postUnemployment
F
7

Use a LinearLayout, add your header upon the ListView and the footer above. Gives the ListView layout_weight="1"

Flowerdeluce answered 29/5, 2013 at 11:5 Comment(1)
That worked and fits right into my linear layout without a lot of changes. ThanksNagual
D
1

Make the header and footer separate Views which you locate over the top and bottom of the ListView. Then set the opacity for those Views.

Diggings answered 29/5, 2013 at 11:5 Comment(0)
L
1

create your custom header and footer with your list view code as below

header.xml file

 <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView      
            android:layout_width="match_parent"
            android:layout_height="your custom height" // you may set default too
          />

    </RelativeLayout>

footer.xml file

 <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RelativeLayout>

add where your listview

 LayoutInflater inflaterHeader = getLayoutInflater();
    ViewGroup header = (ViewGroup) inflaterFooter.inflate(
                    R.layout.header, list_view, false);
    yourListView.addHeaderView(header);

    LayoutInflater inflaterFooter = getLayoutInflater();
            ViewGroup footer = (ViewGroup) inflaterFooter.inflate(
                    R.layout.footer, list_view, false);
    yourListView.addFooterView(footer);
Lucero answered 3/9, 2014 at 13:14 Comment(0)
C
1

we can set header and footer in this way also, i am setting header layout above listview and in same way we cans et footer below listview

<LinearLayout
        android:id="@+id/ly_header"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/app_theme_color"
        android:orientation="horizontal">
        <include layout="@layout/header_icuc"/>
    </LinearLayout>

    <ListView
        android:id="@+id/lv_contacts"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/ly_header"
        android:background="#F3F4F6"
        android:divider="@drawable/contact_list_divider"
        android:dividerHeight="2dp"
        android:scrollbars="none" />
Chapnick answered 6/4, 2017 at 14:50 Comment(0)
R
-1

Create your own custom View using LinearLayout and ListView

Risser answered 29/5, 2013 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.