在LinearLayout中具有自定义适配器的Android ListView-无法正确滚动

在LinearLayout中具有自定义适配器的Android ListView-无法正确滚动,android,android-layout,Android,Android Layout,我有一个包含几个文本和按钮视图以及两个列表视图的布局。这些列表视图由项目动态填充-每个项目都是带有几个texview的线性布局,或者是一个TextView和一个ImageView 简化版: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation=

我有一个包含几个文本和按钮视图以及两个列表视图的布局。这些列表视图由项目动态填充-每个项目都是带有几个texview的线性布局,或者是一个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的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中的单个元素

我也尝试过用表格布局代替线性布局。我尝试了许多不同的方法,包括使用和不使用scrollview,但以下一种情况一直在发生:

  • 每个ListView中的元素都可以滚动,而整个页面基本上都在适当的位置(一些视图被部分推离屏幕)
  • 所有内容都会占用所需的所有空间,但页面不可滚动,元素会完全脱离屏幕
必须将列表视图置于滚动视图之外。如果列表中的项目超过屏幕大小,将滚动列表视图。一旦您将列表视图置于滚动视图之外,则其中任何一个视图都将单独滚动


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

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



您不应该将ListView放在ScrollView中,尝试摆脱ListView,它应该工作得更好。将scrollable放在scrollable中,同样,带有height=wrap的ListView只不过是一个线性布局。我不希望列表视图可以单独滚动,只希望整个屏幕(滚动视图)。另一张海报上说“ListView with height=wrap_content只不过是一个linearlayout”——但我无法设置一个自定义适配器,在linearlayout上扩展arrayAdapter。但一旦我找到解决办法,我会发布我的解决方案。这是朝着正确方向迈出的一步。我不知道ListView天生是可滚动的,不能进入滚动视图。
<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>