android - 如何根据 ScrollView 中的项目设置 ListView 高度

标签 android android-layout listview android-scrollview

我正在尝试在 scrollView 中添加 listView 和。

问题是我无法以编程方式设置 ListView 高度并正确重绘 ScrollView,因为它下面的项目在更改高度之前仍处于旧位置。

XML

<ScrollView
    android:id="@+id/movie_detail_scroll_view"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/movie_detail"
        style="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipChildren="false">
        ...........
        <ListView
            android:id="@+id/trails_list_view"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/movie_videos_container"
            android:choiceMode="none"/>
        <include
            android:id="@+id/movie_reviews_container"
            layout="@layout/partial_movie_reviews"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/trails_list_view"/>
        ............
    </RelativeLayout>
</ScrollView>

我用来调整 listView 大小的方法:

/**
 * Sets ListView height dynamically based on the height of the items.
 *
 * @param listView to be resized
 * @return true if the listView is successfully resized, false otherwise
 */
public static boolean setListViewHeightBasedOnItems(ListView listView, ViewGroup container) {

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

        //redraw the container layout.
        container.requestLayout();

        return true;

    } else {
        return false;
    }

}

结果: enter image description here

即使重绘 ScrollView

,我也无法摆脱这个空闲空间

我该怎么办?

最佳答案

这对聊天 100% 有效,即使元素的高度不同也是如此。

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);
            float px = 500 * (listView.getResources().getDisplayMetrics().density);
            item.measure(View.MeasureSpec.makeMeasureSpec((int) px, View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            totalItemsHeight += item.getMeasuredHeight();
        }

        // Get total height of all item dividers.
        int totalDividersHeight = listView.getDividerHeight() *
                (numberOfItems - 1);
        // Get padding
        int totalPadding = listView.getPaddingTop() + listView.getPaddingBottom();

        // Set list height.
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalItemsHeight + totalDividersHeight + totalPadding;
        listView.setLayoutParams(params);
        listView.requestLayout();
        //setDynamicHeight(listView);
        return true;

    } else {
        return false;
    }

}

关于android - 如何根据 ScrollView 中的项目设置 ListView 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35115788/

相关文章:

android - 如何替换android中的应用程序停靠栏图标

android - 在 ListView 的顶部显示新项目

Android 应用程序方向改变行为

android - 检测到堆栈损坏,dalvik VM 崩溃

android - 将 NavigationView 移动到工具栏下后如何隐藏其顶部阴影?

Android:如何像ListView一样用一个TextView快速滚动ScrollView?

java - 添加新记录时 ListView 不刷新

android - 版本冲突更新为Play-Services 9.4.0 Android Studio 2.2

android - RenderScript - 在完成后创建的每个 Allocation 对象上调用 Allocation.destroy() 是一个好习惯吗?

android - 为什么 RecyclerView 项目中的 ConstraintLayout 比 LinearLayout 慢?