【问题标题】:Listview android - change height after data load dynamicallyListview android - 动态加载数据后更改高度
【发布时间】:2016-01-11 07:31:52
【问题描述】:

我有一个这样的 android 布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/headingView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:paddingTop="@dimen/value_5dp">

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

        <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" >

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/relativeLayout1"
            android:layout_centerHorizontal="true"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingBottom="5dp"
                android:paddingLeft="10dp"
                android:paddingTop="5dp"
                android:visibility="gone" />

        <LinearLayout
            android:id="@+id/list_items"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@color/mt_gray_color"
            android:fadeScrollbars="false" >

            <TextView
                android:id="@+id/itemHeading"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"

                android:textStyle="bold"
                android:background="#f2f2f2"
                android:layout_marginTop="15dp"
                android:visibility="gone"/>

            <ListView
                android:id="@+id/mylist"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="@dimen/value_5dp"
                android:minHeight="500dp" />

        </LinearLayout>
    </LinearLayout>

</ScrollView>
  • 加载此页面/片段后,我正在 ListView 中动态加载数据。

  • 列表视图包含多个元素,我期待它会在列表视图中显示列表而不滚动,因为我已将其设置为 WRAP_CONTENT。

我可能有错误的理解以及我想要理解的内容,并找到了一个解决方案,我如何才能避免滚动条并在没有滚动的情况下在列表视图中列出所有项目。

提前致谢。

【问题讨论】:

  • ListView 的概念是列出 Items 。每行都是单独的实体,包含默认或用户定义的布局。您的问题有点令人困惑,您要实现的目标以及实现目标所面临的障碍

标签: android android-layout listview android-listview


【解决方案1】:

您可以在运行时计算 ListView 的所有子代的大小,然后将其高度设置为所有子代的总高度。你可以使用这个方法:

public void justifyListView(ListView listView) {

    ListAdapter adapter = listView.getAdapter();

    if (adapter == null) {
        return;
    }
    ViewGroup vg = listView;
    int totalHeight = 0;

    for (int i = 0; i < adapter.getCount(); i++) {
        View listItem = adapter.getView(i, null, vg);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    ViewGroup.LayoutParams par = listView.getLayoutParams();
    par.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
    listView.setLayoutParams(par);
    listView.requestLayout();
}

【讨论】:

    【解决方案2】:

    当数据发生变化时,可以改变listView的高度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-31
      • 2015-06-01
      • 2013-10-07
      • 2011-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多