2012-02-13 90 views
4

默認情況下,Android不支持ListView上的節標題。根據我的研究,我已經瞭解到有兩種方法可以在ListView上實現節標題。第一個是在列表視圖中支持2種類型的視圖:分隔視圖和內容視圖。第二個是在所有內容視圖中包含分隔視圖,並將分隔視圖的可見性設置爲VISIBLE或GONE。我正在使用第二種方法。如何在ListView中始終粘貼頂部的節標題?

但是,當我向上滾動ListView時,節標題也向上滾動。我知道這很正常。但是,只要屬於它的部分的某些數據仍然顯示,我希望節標題保持在最前面。我想要完成的行爲與iOS中UITableView的部分標題行爲類似。我怎樣才能做到這一點?

另外,我想說明一點,我已經閱讀了一個解決方案,就是在ListView上創建一個視圖,並在需要時更改它。但是,這不適用於所有手機。例如,三星手機上的ListView彈跳。如果我在ListView上方放置一個視圖作爲標題並且ListView彈出,則虛擬標題視圖不會與ListView一起反彈。此外,默認ListView的頂部可以很容易地被發現,因爲當ListView被滾動時它會發光。無論如何要完成這種效果,同時確保它看起來很自然嗎?

下圖爲我會遇到,如果我只是添加在列表視圖頂部的TextView的問題:(從三星Galaxy S2採用)

enter image description here

+0

爲什麼不會工作的呢?只需設置垂直方向的線性佈局TextView標題作爲第一個視圖,而你的ListView作爲第二個視圖。這應該通過滾動來解決您的問題。 – 2012-02-13 09:47:22

+0

@SergeyBenner:它不起作用,因爲當你在某些手機上向下滾動列表視圖(例如,在三星)時,會出現列表視圖最頂部和原始位置之間的間隙。我添加了一張圖片來演示我所指的行爲。 – Arci 2012-02-14 02:27:59

回答

0

看到在這個XML +符號靜態U將得到一些幫助

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

+0

查看圖片http://postimage.org/image/gr8ggl71j/ – 2012-02-14 05:00:12

+0

謝謝!但是,您如何捕捉ScrolLView上的onScroll事件?你怎麼知道在ScrollView上顯示的最上面的項目?我正在使用API​​ 8。 – Arci 2012-02-14 09:39:18

相關問題