【问题标题】:Android ListView with fixed header and footer具有固定页眉和页脚的 Android ListView
【发布时间】:2013-05-29 11:04:08
【问题描述】:

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

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

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

【问题讨论】:

    标签: android android-listview customization


    【解决方案1】:

    我通过使用@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>
    

    设备截图

    【讨论】:

      【解决方案2】:

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

      inlfating布局@Android listview with header and footer buttons的方法使用示例

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

      您可以按照@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>
      

      图形布局快照

      【讨论】:

      • 效果很好。我使用了这个解决方案,但在页眉/页脚下方和上方添加了一个带有平铺背景的透明 ImageView。
      • @Goran 如果有帮助,请单击勾选一次来标记答案。它将帮助其他访问该帖子的人
      【解决方案3】:

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

      【讨论】:

      • 这很有效,并且非常适合我的线性布局,无需进行大量更改。谢谢
      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        使用您的列表视图代码创建您的自定义页眉和页脚,如下所示

        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>
        

        footer.xml 文件

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

        【讨论】:

          【解决方案6】:

          我们也可以通过这种方式设置页眉和页脚,我在列表视图上方设置页眉布局,同样我们可以在列表视图下方设置页脚

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

          【讨论】:

            【解决方案7】:

            使用 LinearLayout 和 ListView 创建您自己的自定义视图

            【讨论】:

              猜你喜欢
              • 2012-11-04
              • 2014-11-05
              • 1970-01-01
              • 1970-01-01
              • 2014-07-07
              • 1970-01-01
              • 1970-01-01
              • 2012-09-17
              • 2014-03-02
              相关资源
              最近更新 更多