Android LinearLayout 中带有自定义适配器的 ListViews - 滚动不正确

Android ListViews with custom adapters within LinearLayout - not scrolling correctly

我有一个包含多个文本和按钮视图以及两个列表视图的布局。这些列表视图动态地填充了项目——每个项目都是一个带有几个 TexView 的 LinearLayout,或者一个 TextView 和一个 ImageView。

简化版:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

            <TextView
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                />

            <ListView
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="wrap_content"/>

            <ListView
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:layout_height="wrap_content"/>

        </LinearLayout>

    </ScrollView>
</LinearLayout>

添加到上述 ListView 的列表视图元素示例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <ImageView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

我希望看到的是可滚动的完整页面。不是每个 ListView 中的单个元素。

我尝试过使用 TableLayout 而不是 LinearLayout 来做同样的事情。我尝试了很多不同的方法,有和没有滚动视图,并且以下情况之一不断发生:

您必须将列表视图置于滚动视图之外。如果列表中的项目超过屏幕大小,列表视图将滚动。一旦你将列表视图放在滚动视图之外,那么它们中的任何一个都将单独滚动。

希望它能解决您的问题。让我知道。

我最终不得不重新考虑我的布局。我决定使用两个可滚动的列表视图。让他们不把东西推离屏幕的关键是将每个列表视图的布局高度设置为 0dp 并将权重设置为 1。 (当然你可以将权重设置为不同的比例,比如 1 和 3,让一个比另一个占据更多 space):

<LinearLayout
    ...
    android:orientation="vertical"
    >
    <ListView
        android:id="@+id/locationsListView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <TextView
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <ListView
        android:id="@+id/locationsListView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <TextView
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>