ScrollView中的ListView具有自动调整高度

时间:2015-12-01 08:11:22

标签: android listview height scrollview onmeasure

我尝试在ListView内创建一个ScrollViewEdiText ListView。所以我使用自定义方法来制作ListView NonScrollable。

我的ListView项目xml是这样的:

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="15dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/title1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="3"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingTop="3dp"
        android:text="Text"
        android:textColor="#222"
        android:textSize="18sp"
        tools:ignore="HardcodedText" />
    <TextView
        android:id="@+id/title2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_below="@+id/title1"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingBottom="2dp"
        android:text="New Text"
        android:textColor="#727272"
        android:textSize="15sp"
        tools:ignore="HardcodedText" />


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title2"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="8dp"
        android:hint="Note"
        android:textSize="13sp"
        android:id="@+id/text_note"/>
</RelativeLayout>

自定义方法代码如下:

public class NonScrollListView extends ListView {

    public NonScrollListView(Context context) {
        super(context);
    }
    public NonScrollListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
}

问题是当我在EditText中创建一个新行时,我的listview高度没有调整,并使我在ListView中的最后一项消失。 屏幕截图如下:

这是我试图做的 Before

但结果是这样的 enter image description here

我从这里看到: Android: Listview in ScrollView with dynamic height

它说将ListView更改为LinearLayout ..有人可以帮助我如何制作吗?我不知道如何将ListView更改为LinearLayout

如果不必更改为LinearLayout,是否有任何方法可以调整ListView的高度,以便我的项目不会消失?

需要帮助来解决这个问题。

2 个答案:

答案 0 :(得分:2)

尝试使用此方法:

 public static void setListViewHeightBasedOnChildren(ListView listView) {
            ListAdapter listAdapter = listView.getAdapter(); 
            if (listAdapter == null) {
                // pre-condition
                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);
        }

喜欢setListViewHeightBasedOnChildren(listView)

答案 1 :(得分:0)

使用以下课程

public class ListViewUtil {
    private static final String CNAME = ListViewUtil.class.getSimpleName();

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

用于调用方法

ListViewUtil.setListViewHeightBasedOnChildren(lview);