Android 显示两个 full-size ListView

Android display two full-size ListViews

我无法显示两个连续的 full-sized(高度)ListViews,它们不能单独滚动,只能滚动根元素。

自从我有各种输出(两个可滚动列表视图、half-cut 滚动视图等)以来,这件事一直困扰着我,但 none 成功了。阅读以前的帖子 here, here and here.

为您提供当前布局 xml,解决方案是什么?删除ScrollView,将LinearLayout更改为RelativeLayout,设置layout_weight?

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    >

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

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >

            <xxx.xxx.xxx.xxx.MultiSpinner
                android:id="@+id/ad_list_filter"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                />

            <xxx.xxx.xxx.xxx.MultiSpinner
                android:id="@+id/ad_list_sort"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                />

        </LinearLayout>

        <ListView
            android:id="@+id/adListInterests"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="3dp"
            android:divider="@null"
            android:dividerHeight="5dp"
            >
        </ListView>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@drawable/ad_list_seperator"
            />

        <ListView
            android:id="@+id/adListNoninterests"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="3dp"
            android:divider="@null"
            android:dividerHeight="5dp"
            >
        </ListView>
    </LinearLayout>
</ScrollView>

提前致谢!

编辑:

删除建议的 ScrollView 后会生成两个可滚动列表。根据评论,我将尝试使用提供的插件来实现它,但我仍然希望找到仅基于修改布局代码的解决方案。感谢您的时间!

图片:

布局:

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >

        <xxx.xxx.xxx.xxx.MultiSpinner
            android:id="@+id/ad_list_filter"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            />

        <xxx.xxx.xxx.xxx.MultiSpinner
            android:id="@+id/ad_list_sort"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            />

    </LinearLayout>
    <ListView
        android:id="@+id/adListInterests"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:divider="@null"
        android:dividerHeight="5dp"
        >
    </ListView>

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/ad_list_seperator"
        />

    <ListView
        android:id="@+id/adListNoninterests"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:divider="@null"
        android:dividerHeight="5dp"
        >
    </ListView>
</LinearLayout>

您的层次结构是正确的,只需像这样更改您的 ListView 代码:

android:layout_height="wrap_content" <!-- cahnge this line -->
android:layout_weight="xxx" <!-- remove this line -->

然而,这是一个糟糕、低效且占用内存的解决方案。通过这样做,您将强制屏幕外的所有视图保留在内存中。更好的解决方案是只使用一个列表,其中包含处理多个 datasets/viewtypes 的自定义适配器和一个包含微调器的 header 视图。

目前对您来说最简单、最快捷、最正确的解决方案是使用 cwac-merge 库,它允许您从多个 ListAdapter 中加载一个 ListView 数据。一旦您了解了它的工作原理并使用了它,您就可以像这样更改布局:

list_header.xml

在将合并适配器分配给列表视图之前,您必须以编程方式设置 header 视图。您可以扩充此布局 XML.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
  <xxx.xxx.xxx.xxx.MultiSpinner.../>
  <xxx.xxx.xxx.xxx.MultiSpinner.../>
</LinearLayout>

list_main.xml

这是您的 activity 或片段的主要布局。它将只包含列表,可以处理您需要的一切。

<?xml version="1.0" encoding="utf-8"?>
<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/adListInterests"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="3dp"
    android:layout_marginRight="3dp"
    android:divider="@null"
    android:dividerHeight="5dp"/>

如何使用cwac-merge

您需要将其导入 Gradle(对于 Android Studio)

repositories {
    maven {
        url "https://repo.commonsware.com.s3.amazonaws.com"
    }
}

dependencies {
    compile 'com.commonsware.cwac:merge:1.1.+'
}

或下载几个 .jars(用于 Eclipse)

代码如下所示:

View header = LayoutInflater.from(context).inflate(R.layout.list_header, null, false);

// get references for your Spinners here...

myListView.addHeader(header, null, false);

// setup your two adapters as you are doing now

MergeAdapter adapter = new MergeAdapter();
adapter.addAdapter(firstAdapter); // the adapter that you previously used for the first list
adapter.addAdapter(secondAdapter); // the adapter that you previously used for the second list
myListView.setAdapter(adapter);

关于有两个单独的 ListView 的整个想法是错误的 - 而不是我构建了一个 ListView 并在第一个之后为以下列表添加了一个自定义行作为 header一个结束了。

描述了我使用的概念here