2013-05-29 100 views
10

如何创建带有固定页眉和页脚的ListView带有固定页眉和页脚的Android ListView

我不希望页眉/页脚滚动ListView中的项目。

页眉/页脚是否可能在ListView上浮动,以便页眉/页脚不需要有直底/顶部背景,并且ListView项目在页眉/页脚视图的背景下滚动,但仍然显示列表的第一个元素?

回答

14

我解决它通过使用@blackbelt建议和小的ImageView与源图像是用TRANSPARANT瓷砖背景。

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

从设备 enter image description here

7

使用LinearLayout,将您的标题添加到ListView和上面的页脚上。给出ListView layout_weight="1"

+1

这工作,适合我的线性布局没有很多的变化。谢谢 – Veeru

1

使页眉和页脚分开您在ListView的顶部和底部定位的视图。然后为这些视图设置不透明度。

11

http://developer.android.com/reference/android/widget/ListView.html#addHeaderView%28android.view.View%29自己的自定义视图。检查这个addHeaderView(参数)。

http://developer.android.com/reference/android/widget/ListView.html#addFooterView%28android.view.View%29。检查此为addFooterView(参数)。

通过@Android listview with header and footer buttons

可以使用addHeaderView和addFooterView为列表添加页眉和页脚inlfating的布局的方法usuage的实施例。

您可以按照@blackbelt的建议进行操作。我使用了相对布局而不是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> 

图形布局抽点

enter image description here

+0

这工作正常。我使用了这个解决方案,但在页眉/页脚下方和上方为锯齿效果添加了透明的ImageView和平铺背景。 – Goran

+0

@戈兰如果有帮助,请点击一下勾号标记答案。它会帮助其他人访问 – Raghunandan

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> 

页脚。xml文件

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

附加在您的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); 
+0

伟大的解决方案。谢谢。 – confile

0

我们可以设置页眉和页脚这样也,我设置上述列表视图,并在下面我们易拉罐等页脚同样的方式头布局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" />