【问题标题】:Set size of ListView base on his non-fixed elements heigth根据他的非固定元素高度设置 ListView 的大小
【发布时间】:2019-06-24 17:21:19
【问题描述】:

我的布局定义如下。

<ScrollView
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <LinearLayout
            android:id="@+id/section1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp"
                android:text="@string/anomaly_products_title"
                android:textColor="@color/title"
                android:textSize="16sp"
                android:textStyle="bold" />

            <ListView
                android:id="@+id/section1_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="40dp"
                android:divider="@android:color/transparent"
                android:dividerHeight="20dp" />
        </LinearLayout>

        .... Some Other section

    </LinearLayout>
</ScrollView>

我还定义了列表的项目如下。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="13dp"
    android:background="@drawable/semi_rounded">

    <CheckBox
        android:id="@+id/checkbox_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp" />
</LinearLayout>

我想将ListView 的高度设置为他所有元素的高度并使其不可滚动。滚动只能发生在父 ScrollView 上。为此,我找到了函数setListViewHeightBasedOnChildrenhere

问题是我的复选框标签可以显示在多行上,因此会稍微增加项目的大小。在这种情况下,如果有许多复选框,ListView 就会变成可滚动的。

有没有办法处理这种情况?

【问题讨论】:

    标签: android listview


    【解决方案1】:

    我建议您在ListView 中添加页眉和页脚,而不是ScrollViewListView 的组合。这很简单,也更容易实现。这是一个nice tutorial,关于如何在ListView 中添加页眉/页脚。

    根据项目数和项目大小处理ListView 的高度是困难且难以优化整个流程的。您也可以考虑使用RecyclerView 而不是ListView。它还可以选择添加页眉和页脚。 Here's an implementation.

    要添加多个列表(在您的情况下为多个部分),您可以考虑查看此示例 Github Project,它可能会引导您了解如何使用 RecyclerView 实现您想要的行为。

    因此,删除ScrollView。将ListView 上方的内容放在单独的布局文件中,并对ListView 下方的内容执行相同操作。然后相应地将它们添加为页眉和页脚。

    【讨论】:

    • 如果滚动视图中有更多不属于列表页脚或页眉的内容怎么办?
    • 你能举个例子吗?我实际上对更多内容感到困惑。正如我从问题中所理解的那样,ListView 位于整个布局的中间。因此,ListView 上方的所有内容都可以设置为页眉,而其下方的所有内容都可以设置为页脚,这是我的建议。
    • 我现在明白了。谢谢你的澄清。您仍然可以使用RecyclerView 来实现这一点。我正在分享一个示例Github Project,我认为它适用于此。
    猜你喜欢
    • 1970-01-01
    • 2015-03-27
    • 2021-09-11
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    相关资源
    最近更新 更多