带有固定头部和底部的安卓ListView

13

如何创建一个带固定头部和底部的 ListView?

我不想让头部/底部随着 ListView 中的项目滚动。

可以把头部/底部浮在 ListView 上面吗?这样头部/底部就不需要拥有直线状的底部/顶部背景,而且 ListView 的项目可以在头部/底部视图的背景下滚动,但仍然显示列表的第一个元素吗?

7个回答

16
我使用了@blackbelt的建议以及一个小的ImageView,源图像是透明的,并带有平铺背景。

我通过使用@blackbelt的建议和一个带有平铺背景的透明源图像的小型ImageView来解决了这个问题。

<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>

设备的屏幕截图 在此输入图片描述


10

http://developer.android.com/reference/android/widget/ListView.html#addHeaderView%28android.view.View%29。请查看此链接以了解如何使用 addHeaderView(param)。

http://developer.android.com/reference/android/widget/ListView.html#addFooterView%28android.view.View%29。请查看此链接以了解如何使用 addFooterView(param)。

Android listview with header and footer buttons中有一个使用该方法的示例,可以通过载入布局来实现。

您可以使用 addHeaderView 和 addFooterView 来为列表添加页眉和页脚。

您可以像@blackbelt建议的那样去做。我使用了相对布局而不是线性布局。

<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>

图形化布局快照

在此输入图片描述


非常好,我使用了这个解决方案,并添加了一个透明的ImageView,带有锯齿状的平铺背景,放置在页眉/页脚上下方。 - Goran
@Goran请点击一次打勾标记答案,如果它有帮助。这将有助于其他访问该帖子的人。 - Raghunandan

7
使用LinearLayout,将标题放在ListView上方,将页脚放在上方。给ListView layout_weight="1"

1
那个可以工作,而且可以完美地适应我的线性布局,不需要进行太多的更改。谢谢。 - Veeru

1
请使用以下代码创建您的自定义页眉和页脚:

header.xml 文件

 <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>
 <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RelativeLayout>

在你的列表视图中添加。
 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);

1
将标题和页脚制作为单独的视图,定位在ListView的顶部和底部。然后设置这些视图的不透明度。

1
我们也可以这样设置头部和底部,我将头部布局设置在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" />

-1
使用LinearLayout和ListView创建自定义视图。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接