ListView仅显示一个高度为"wrap_content"的元素. [英] ListView only one element shown with height = "wrap_content"

查看:68
本文介绍了ListView仅显示一个高度为"wrap_content"的元素.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的片段布局上,我有一个嵌套滚动视图,里面有两个相对布局,顶部和底部,在底部,我有一个带有大约二十个元素的列表视图,但是如果我用wrap_content设置列表视图的高度,则只有一行例如,如果将高度设置为1000dp,则显示所有行,我不想以静态方式固定高度,而是希望使用wrap_content,该怎么办?

On my fragment layout I have a Nested Scroll view with two relative layout inside, top and bottom, on the bottom I have a listview with about twenty elements, but if I set the height of the listview with wrap_content, only one line is shown, whereas if I set the height to 1000dp, for example, all the lines are shown, I wouldn't want to fix the height in a static way but I'd rather use wrap_content, what can I do?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/nested_content"
    android:clipToPadding="false"
    android:scrollbars="none"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:id="@+id/top">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        //some button and text view
    </RelativeLayout>
  </RelativeLayout>

  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bottom"
        android:layout_below="@id/top">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/scores"
        </ListView>

    </RelativeLayout>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>

推荐答案

您可以使用以下Utility方法根据子计数来设置 ListView 的高度.

You can use below Utility method to set the height of your ListView based on child count.

public static void setListViewHeightBasedOnChildren(ListView myListView) {
        ListAdapter adapter = myListView.getAdapter(); 
        if (myListView != null) {
           int totalHeight = 0;
           for (int i = 0; i < adapter.getCount(); i++) {
              View item= adapter.getView(i, null, myListView);
              item.measure(0, 0);
              totalHeight += item.getMeasuredHeight();
           }

           ViewGroup.LayoutParams params = myListView.getLayoutParams();
           params.height = totalHeight + (myListView.getDividerHeight() * (adapter.getCount() - 1));
           myListView.setLayoutParams(params);
        }          
    }

这篇关于ListView仅显示一个高度为"wrap_content"的元素.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆