2013-05-29 96 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" />