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

时间:2015-04-08 10:34:28

标签: 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>

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

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

2 个答案:

答案 0 :(得分: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();

}

设置适配器时调用它。

答案 1 :(得分:2)

我在这里找到了这个解决方案:Dynamically change the ListView height - Android对我有用(虽然我不知道它是否是最好的方式因为我觉得它会更简单......)。 / p>

以下是代码(如果链接没有做到):

import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.ListAdapter;
import android.widget.ListView;

public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter == null) {
        // pre-condition
        return;
    }

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

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