【问题标题】:How to implement 2 listViews with a scrollview when scrollView is defined "below the header to above the footer"?当scrollView被定义为“在页眉之下到页脚之上”时,如何用一个scrollview实现2个listViews?
【发布时间】:2026-01-09 23:30:01
【问题描述】:

布局结构是

<header>
<scrollView>
<footer>

页眉和页脚是固定的,它们不应移动。但是 scrollView 中的内容需要相应地滚动。

ScrollView 内容是

listView1
layout - this layout has few labels
listView2
textView

所有的 ScrollView 内容也被包裹在一个布局中,并作为一个单独的子项提供给 scrollView。请提供相同的 XML 文件。

【问题讨论】:

    标签: android listview android-scrollview


    【解决方案1】:

    使用RelativeLayout 作为父级,然后将您的页眉布局和layout_below 放在滚动视图下方,layout_below 您的页脚添加您的alignParentTop 和alignParentBottom 以确保它们在那里。我没有对此进行测试,但它应该可以工作。

    <RelativeLayout
            android:id="@+id/header"
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></RelativeLayout>
        <ScrollView
            android:id="@+id/scroll"
            android:layout_below="@id/header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <ListView
                    android:id="@+id/lv1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"></ListView>
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"></RelativeLayout>
                <ListView
                    android:id="@+id/lv2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"></ListView>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </RelativeLayout>
    
        </ScrollView>
        <RelativeLayout
            android:id="@+id/footer"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></RelativeLayout>
    </RelativeLayout>
    

    【讨论】:

    • 它只给出每个列表视图的一项
    • 将内部相对布局改为线性布局并使用权重,很快就会更新答案
    • 尝试将其更改为线性布局,但也没有运气。
    • 尝试 android.support.v4.widget.NestedScrollView 而不是 ScrollView 禁用两个列表视图的 Nestedscroll
    最近更新 更多