MaxK
MaxK

Reputation: 437

How to "anchor" headerview in ListView

I'm trying to "anchor" the headerview for my listview, meaning when I scroll down through the list I do not want the header to scroll with it. I want the header to stay stationary at all times. As you all know this is a small code snippet used to place the header to the Listview:

                    lv = (ListView)v.findViewById(R.id.lv1);

                View header = (View)inflater.inflate(R.layout.listview_header, null);
                lv.addHeaderView(header);

I thank you for any and all help in advance.

Upvotes: 0

Views: 719

Answers (1)

Siddharth Lele
Siddharth Lele

Reputation: 27748

A header to a ListView will always scroll with the ListView. The simplest thing you can do is create a layout, for example, like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/phone_thing_comment_bg"
            android:gravity="center"
            android:paddingBottom="5dp"
            android:paddingLeft="9dp"
            android:paddingRight="9dp"
            android:paddingTop="5dp" >

            <EditText
                android:id="@+id/editFilterList"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_margin="4dp"
                android:background="@drawable/phone_thing_comment_box"
                android:drawableLeft="@drawable/filter_search_icon"
                android:drawablePadding="5dp"
                android:hint="Type Friends Name"
                android:inputType="text"
                android:maxLines="1"
                android:textColor="#ff333333"
                android:textColorHint="#ff78797d"
                android:textCursorDrawable="@null" >
            </EditText>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <ListView
                android:id="@+id/list"
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:cacheColorHint="@android:color/transparent"
                android:divider="#000000"
                android:dividerHeight="0dp"
                android:fadingEdge="none"
                android:persistentDrawingCache="scrolling"
                android:scrollbars="none" >
            </ListView>

            <TextView
                android:id="@android:id/empty"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:gravity="center"
                android:padding="10dp"
                android:text="@string/no_friends"
                android:textColor="#f1f1f1"
                android:textSize="15sp"
                android:textStyle="bold"
                android:visibility="gone" >
            </TextView>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

This way, in this example, the EditText is always visible at the top and just the ListView scrolls.

Upvotes: 1

Related Questions