【问题标题】:ListView inside ScrollView with Auto Adjust Height具有自动调整高度的 ScrollView 内的 ListView
【发布时间】:2016-03-05 02:05:24
【问题描述】:

我尝试在ScrollView 内部创建一个ListView,并在ListView 内部创建一个EdiText。所以我使用自定义方法使我的ListView NonScrollable。

我的 ListView Item 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 中的最后一个项目消失。 截图是这样的:

这是我尝试做的

但是结果是这样的

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

它说将ListView 更改为LinearLayout.. 有人可以帮我怎么做吗?我不知道如何将ListView 更改为LinearLayout

如果不必更改为LinearLayout,有什么办法可以调整我的ListView 的高度,这样我的项目就不会消失?

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

【问题讨论】:

  • 可以分享列表项的xml视图吗?
  • @Ajitha 我已经添加了我的列表项 xml,你能帮我解决这个问题吗?

标签: android listview height scrollview onmeasure


【解决方案1】:

试试这个方法:

 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)

【讨论】:

  • 我之前已经尝试过解决方案.. 但是当我更改我的 editText 时如何使用它?我在 EditText TextWatcher 中写过这个setListViewHeightBasedOnChildren(listView) 吗?
  • 我已经尝试将其放入public void afterTextChanged(Editable s),但仍然相同.. 我的 ListView 可以自动调整高度。我放错地方了吗?
  • 就像我解释的那样,把我的图像放在顶部。ListView 自动调整高度,无论我在我的 EditText 中创建一个新行。上面的代码不起作用,无法调整我的列表视图的高度
【解决方案2】:

使用下面的类

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

【讨论】: