简体   繁体   中英

How to make a section header in ListView which always sticks on top?

Android does not have a support for section headers on ListView by default. Based from my research, I've learned that there are 2 ways to implement a section header on ListView. The first one is to support 2 types of views in the list view: the separator view and the content view. The second one is to include a separator view in all of the content views and just set the visibility of the separator view to either VISIBLE or GONE. I'm using the second method.

However, when I scroll the ListView upwards, the section header is also scrolled up. I know this is normal. But I want the section header to remain on top as long as some data belonging to its section are still being displayed. The behavior that I want to accomplish is similar to the behavior of the section headers of UITableView in iOS. How can I accomplish this?

Also, I would like to note that I've read that one solution for this is to create a view above the ListView and just change it if needed. However, this won't work for all phones. For example, the ListView on Samsung phones are bouncing. If I placed a view above the ListView to act as header and the ListView bounces, the dummy header view won't bounce together with the ListView. Also, the top of the default ListView can easily be spotted because it glows when the ListView is being scrolled. Is there anyway to accomplish the said effect while making sure that it still looks natural?

The picture below shows the problem that I will be encountering if I just add a textview on top of the listview: (Taken from Samsung Galaxy S2)

在此输入图像描述

see in this xml + symbol is static u will get some help

                   <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
    android:id="@+id/linearlayout">
<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:background="#00ffff">
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/plus" />
</LinearLayout>
<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:orientation="vertical">
    </LinearLayout>
</ScrollView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM