【问题标题】:Show list in listview in full在列表视图中完整显示列表
【发布时间】:2019-01-12 12:38:46
【问题描述】:

我在 ConstraintLayout 中有一个列表视图,它必须显示我的所有列表都已满,并使约束布局更大。我发现的所有选项都不适用于我的情况。

我试图不完全显示列表视图的所有这些选项,每个选项都会留下一半或更多的项目来滚动

这是我的 xml:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.constraint.ConstraintLayout
                android:id="@+id/question_content_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="14dp"
                android:layout_marginTop="59dp"
                android:layout_marginEnd="14dp"
                android:layout_marginBottom="22dp"
                android:background="@color/white"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias="0.0">

                ...

                <ListView
                    android:id="@+id/answers_list"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="12dp"
                    android:layout_marginTop="18dp"
                    android:layout_marginEnd="12dp"
                    android:footerDividersEnabled="false"
                    android:layout_marginBottom="7dp"
                    android:divider="@drawable/divider"
                    android:scrollbars="none"
                    android:transcriptMode="alwaysScroll"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/question_tv"
                    app:layout_constraintVertical_bias="0.0" />

                ...

            </android.support.constraint.ConstraintLayout>

           ...

        </android.support.constraint.ConstraintLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>

我已经试过了:

public static boolean setListViewHeightBasedOnItems(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter != null) {
        int numberOfItems = listAdapter.getCount();
        // Get total height of all items.
        int totalItemsHeight = 0;
        for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
            View item = listAdapter.getView(itemPos, null, listView);
            item.measure(0, 0);
            totalItemsHeight += item.getMeasuredHeight();
        }
        // Get total height of all item dividers.
        int totalDividersHeight = listView.getDividerHeight() *
                (numberOfItems - 1);
        // Set list height.
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalItemsHeight + totalDividersHeight;
        listView.setLayoutParams(params);
        listView.requestLayout();
        return true;
    } else {
        return false;
    }

和

public void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null)
            return;
        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
        int totalHeight = 0;
        View view = null;
        int itemCount = listAdapter.getCount();
        for (int i = 0; i <= itemCount; i++) {
            view = listAdapter.getView(i, view, listView);
            if (i == 0)
                view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
            view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED );
            totalHeight += view.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = (totalHeight + (listView.getDividerHeight() * (itemCount - 1)));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }

【问题讨论】:

    标签: java android listview layout android-listview


    【解决方案1】:

    让我拥有一个列表视图并能够滚动浏览它的方法是将滚动视图作为父视图,并在滚动视图内部设置约束布局。

    【讨论】:

    • 我最多有 10 个项目,只有一个文本视图。所以我认为这对性能来说没什么大不了的。 ListView 就够了
    • 该死..我发现它仍然无法正常工作..如果 listview 完整显示并从屏幕上消失,则 scollview 被锁定,我无法滚动它,有时当 listview 大于它应该适合不完整显示的屏幕列表视图。
    • 我只是将 listview 与 recyclerview 交换,它已经开始完美运行。魔法……
    【解决方案2】:

    就我而言,我发现的唯一解决方案是将 ListView 与 RecylerView 交换;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      • 2023-01-04
      • 1970-01-01
      相关资源
      最近更新 更多