如何在向其添加项目后使ListView的高度增长?

Lir*_*man 1 android-listview

我有这个xml,我有一个列表视图,我在按钮点击时添加项目.问题是ListViews的高度没有增长,我只能看到一个项目......

这是我的xml:

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

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/scrollView">

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

        <LinearLayout android:tag="Order Status"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Spinner
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
            <TextView
                android:layout_width="@dimen/field_label_width"
                android:layout_height="wrap_content"
                android:text="Status"/>
        </LinearLayout>

        <ListView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/lstItems"></ListView>
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"                            
            android:src="@android:drawable/ic_input_add"
            android:onClick="btnAddItemToList"/>
    </LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

如果重要,列表视图项有:2 EditTexts,1 TextView和一个按钮,所以我使用自定义适配器.

如何使列表视图高度相应增长?

Nis*_*arg 6

private void setListViewHeightBasedOnChildren(ListView listView) {
    Log.e("Listview Size ", "" + listView.getCount());
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {

        return;
    }

    int totalHeight = 0;
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight
            + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();

}
Run Code Online (Sandbox Code Playgroud)

设置适配器时调用它.