Android: Show listview between header and footer

2019-02-27 19:03发布

问题:

I learned from android dev tutorial and now I can make ListView. and it worked perfectly fine. Now my requirement is I want to show listview with a header and footer which I have made in xml file.

Basically on the top there will be a header & footer (a text view) and then follows listview scrollable between header and footer

Can someone forward me to the appropriate tutorial.

回答1:

Here is the tutorial links:

http://blog.maxaller.name/2010/05/attaching-a-sticky-headerfooter-to-an-android-listview/

http://www.vogella.de/articles/AndroidListView/article.html

http://www.vogella.de/articles/AndroidListView/article.html#headerfooter



回答2:

You may need to add in your xml two TextViews. One before your ListView, the other one, right under the ListView.



回答3:

here comes a snippet for the ListView with header+footer;

<LinearLayout android:id="@+id/lay_listitems"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:background="@drawable/white"
                >

                <TextView android:id="@+id/listview_items_header"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="@dimen/text_size_large"
                    android:textStyle="normal"
                    android:gravity="center_horizontal"
                    android:textColor="@drawable/titlecolor"
                    android:singleLine="true"
                    android:visibility="visible"
                    android:text=" --- HEADER ---"
                    />
                <ListView android:id="@+id/listview_items"
                    android:layout_width="match_parent" 
                    android:layout_height="match_parent"
                    android:drawSelectorOnTop="false"
                    android:smoothScrollbar="true"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:clickable="true"
                    android:dividerHeight="1dip"
                    android:divider="@drawable/ltgray"
                    android:layout_gravity="center"
                    android:gravity="center"
                    />

                <TextView android:id="@+id/listview_items_footer"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="@dimen/text_size_large"
                    android:textStyle="italic"
                    android:gravity="center_horizontal"
                android:textColor="@drawable/normaltextcolor"
                android:singleLine="true"
                android:visibility="visible"
                android:text=" --- FOOTER --- "
                    />
            </LinearLayout>

p.s. as an extra, custom colors can be added into colors.xml as below

<drawable name="titlecolor">#83a4cd</drawable>
<drawable name="normaltextcolor">#83a4cd</drawable>
<drawable name="gray">#585858</drawable>
<drawable name="ltgray">#BDBDBD</drawable>
<drawable name="extraltgray">#F2F2F2</drawable>


回答4:

This may be late but hope this may help someone. :-)

<?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:orientation="vertical">
    <TextView
        android:text="Header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <ListView
        android:id="@android:id/listview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="Footer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom"/>
</LinearLayout>