2013-05-29 37 views
10

Làm cách nào để tạo một ListView với đầu trang và chân trang cố định?Android ListView với đầu trang và chân trang cố định

Tôi không muốn đầu trang/chân trang để cuộn với các mục trong ListView. Có thể là đầu trang/chân trang đang trôi nổi trên ListView sao cho đầu trang/chân trang không cần phải có nền đáy/trên cùng thẳng và các mục ListView cuộn bên dưới nền của chế độ xem đầu trang/chân trang, nhưng vẫn hiển thị phần tử đầu tiên của danh sách?

Trả lời

14

Tôi giải quyết nó bằng cách sử dụng gợi ý @blackbelt và một ImageView nhỏ với hình ảnh nguồn là transparant với một nền gạch.

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

Ảnh chụp màn hình từ điện thoại enter image description here

7

Sử dụng LinearLayout, thêm tiêu đề của bạn trên ListView và chân trang ở trên. Cung cấp số điện thoại ListView layout_weight="1"

+1

Điều đó phù hợp với bố cục tuyến tính của tôi mà không có nhiều thay đổi. Cảm ơn – Veeru

1

Đặt các chế độ xem riêng biệt cho đầu trang và chân trang mà bạn xác định trên đầu và cuối của ListView. Sau đó đặt độ mờ cho các Chế độ xem đó.

-1

Tạo chế độ xem tùy chỉnh của riêng bạn bằng LinearLayout và ListView

11

http://developer.android.com/reference/android/widget/ListView.html#addHeaderView%28android.view.View%29. Kiểm tra điều này cho addHeaderView (param).

http://developer.android.com/reference/android/widget/ListView.html#addFooterView%28android.view.View%29. Kiểm tra điều này cho addFooterView (param).

Ví dụ về phương pháp usuage bởi inlfating một bố cục @Android listview with header and footer buttons

Bạn có thể sử dụng addHeaderView và addFooterView cho danh sách để thêm header và footer.

Bạn có thể làm như những gì @blackbelt đề xuất. Tôi đã sử dụng bố cục tương đối thay vì 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> 

Giao diện đồ họa Snap Shot

enter image description here

+0

Điều đó hoạt động tốt. Tôi đã sử dụng giải pháp này nhưng đã thêm một ImageView trong suốt với nền gạch cho hiệu ứng răng cưa bên dưới và phía trên đầu trang/chân trang. – Goran

+0

@Goran pls đánh dấu câu trả lời bằng cách nhấp vào đánh dấu một lần nếu nó giúp. Nó sẽ heps những người khác truy cập vào bài viết – Raghunandan

1

tạo tiêu đề tùy chỉnh của bạn và footer với mã xem danh sách của bạn như sau

tập tin 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> 

chân .tập tin xml

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

add nơi listview của bạn

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

giải pháp tuyệt vời. Cảm ơn bạn. – confile

0

chúng ta có thể thiết lập header và footer theo cách này cũng được, tôi đang thiết bố trí tiêu đề trên listview và trong cùng một cách mà chúng tôi lon et chân dưới đây 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" />