【问题标题】:Android display two full-size ListViewsAndroid显示两个全尺寸ListView
【发布时间】:2015-03-01 00:14:30
【问题描述】:

我无法显示两个连续的全尺寸(高度)ListViews,它们不能单独滚动,只能通过滚动根元素来滚动。

这件事整天困扰着我,因为我有各种输出(两个可滚动列表视图、半切滚动视图等)但没有一个成功。阅读之前的帖子hereherehere

为您提供当前的布局 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 后会生成两个可滚动列表。根据 cmets 的说法,我会尝试使用提供的插件来实现它,但是我仍然希望找到仅基于修改布局代码的解决方案。感谢您的时间!

图片:

布局:

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

【问题讨论】:

  • Removal of ScrollView 强烈推荐(这确实是一种更糟糕的做法)。 ListViews that would not be scrollable individually but only by scrolling the root element 是什么意思?也许,您可以重新考虑您的 UI 以使用 ExpandableListView(父/子关系)?
  • @DerGolem 只是想说列表应该完全显示并且滚动必须在 ScrollView 上发生。
  • 您是否知道将 ListView(或任何其他 scrollable)放入 ScrollView(或任何其他 scrollable)是一个错误 想法?
  • 是的。之所以如此,是因为当滚动事件被触发时,使用哪个视图“混淆” - ScrollViewListView
  • 是的,滚动中存在“冲突”。我也认为有一定的 CPU 开销。

标签: android android-layout android-listview


【解决方案1】:

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

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

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

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

list_header.xml

在将合并适配器分配给列表视图之前,您必须以编程方式设置标题视图。您可以扩充此布局 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

这是您的活动或片段的主要布局。它将只包含列表,它可以处理您需要的所有内容。

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

【讨论】:

  • 谢谢,但我可以不使用任何库来实现吗?只是重构代码?
  • 为什么不使用这个库?它以.jar 的形式分发,您可以轻松地将其导入 Android Studio 和 Eclipse。我更新了如何使用它的答案,它真的是一段代码。在任何情况下,您都应该从我的答案中复制布局,如果您不使用该库,则必须实现自定义 BaseAdapter
  • 非常感谢,会试一试。
【解决方案2】:

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

我使用的概念描述为here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-19
    • 2014-11-19
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多