如何在ListView中创建一个始终固定在顶部的区域标题?

4

默认情况下,Android不支持ListView上的部分标题。根据我的研究,我了解到有两种方法可以在ListView上实现部分标题。第一种方法是在列表视图中支持2种视图:分隔视图和内容视图。第二种方法是在所有内容视图中包含一个分隔视图,只需设置分隔视图的可见性为VISIBLE或GONE即可。我正在使用第二种方法。

然而,当我向上滚动ListView时,部分标题也会向上滚动。我知道这很正常。但只要其部分数据仍在显示,我希望部分标题保持在顶部。我想要实现的行为类似于iOS中UITableView的部分标题的行为。我该如何实现呢?

此外,我想指出,我已经阅读到其中一种解决方案是在ListView上方创建一个视图,如果需要,只需更改它。然而,这并不能适用于所有手机。例如,三星手机上的ListView反弹。如果我在ListView上面放置一个视图作为标题,并且ListView弹跳,虚拟的标题视图将不会与ListView一起弹跳。此外,当ListView被滚动时,可以轻松地发现默认ListView的顶部,因为它会发光。有没有办法在确保看起来自然的同时实现所说的效果呢?

下图显示了如果我只在列表视图顶部添加一个文本框将遇到的问题:(来自三星Galaxy S2)

enter image description here


为什么它不起作用?只需使用垂直方向的LinearLayout,将您的TextView标题设置为第一个视图,将您的ListView设置为第二个视图即可。这应该解决您滚动的问题。 - Sergey Benner
@SergeyBenner:它不会起作用,因为在某些手机上(例如三星),当您向下过度滚动listview时,列表视图最顶部和其原始位置之间会出现间隙。我已添加一张图片来演示我所指的行为。 - Arci
1个回答

0

看到这个xml +符号是静态的,您会得到一些帮助

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


谢谢!但是如何在ScrollView上捕获onScroll事件?以及如何知道正在显示在ScrollView上的最顶部的项目?我正在使用API 8。 - Arci

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接