隐藏的listview没有显示所有元素

时间:2013-07-08 10:26:04

标签: android android-layout android-listview

我有一个布局,其中有一些标签可以打开和关闭一些额外的信息。其中一个标签显示了一个列表视图。但是它不是显示所有元素,而是一个接一个地显示启用滚动。我只是想在列表视图中显示所有项目。

屏幕的正常状态:

http://i39.tinypic.com/2n1baq1.png

点击标签后:

http://i41.tinypic.com/2dvliqq.png

这是屏幕的关键xml:     

        <TextView
            android:id="@+id/tv_school"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/tv_basic"
            android:layout_below="@+id/divider3a"
            android:paddingBottom="10dip"
            android:paddingTop="10dip"
            android:text="School Info"
            android:textColor="#5E5E60"
            android:onClick="animationText"
            android:clickable="true" />

        <ImageView
            android:id="@+id/uparrow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/divider3a"
            android:layout_above="@+id/divider3b"
            android:layout_marginRight="10dip"
            android:layout_marginTop="10dip"
            android:layout_marginBottom="10dip"
            android:src="@drawable/up"
            android:visibility="invisible" />

        <ImageView
            android:id="@+id/downarrow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/divider3a"
            android:layout_above="@+id/divider3b"
            android:layout_marginRight="10dip"
            android:layout_marginTop="10dip"
            android:layout_marginBottom="10dip"
            android:src="@drawable/down" />

        <ImageView
            android:id="@+id/divider3b"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@+id/tv_school"
            android:visibility="invisible"
            android:background="@android:color/darker_gray"/>

        <ListView
            android:id="@+id/lv_schoolinfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/divider3b"
            android:layout_alignLeft="@+id/tv_basic"
            android:paddingBottom="10dip"
            android:paddingTop="10dip"
            android:paddingRight="10dip"
            android:scrollbars="none"
            android:orientation="vertical"
            android:visibility="invisible" />


        <ImageView
            android:id="@+id/divider4"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_below="@+id/tv_school"
            android:background="@android:color/darker_gray"/>

这是适配器的代码:

ListView list = (ListView) findViewById(R.id.lv_schoolinfo);
ArrayList<Param> params = l.getSchools();
ArrayList<String> schools = new ArrayList<String>();
for(int i=0; i<params.size(); i++){
    schools.add(params.get(i).getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, schools);
list.setAdapter(adapter);

1 个答案:

答案 0 :(得分:0)

将此类用作实用程序

public class Utility 
{
    public static void setListViewHeightBasedOnChildren(ListView listView) 
        {
            ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null) 
                {
                    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))+20;
            listView.setLayoutParams(params);
            listView.requestLayout();
        }
}

并使用此类

list1.setAdapter(imageAdapteritem);
Utility.setListViewHeightBasedOnChildren(list1);

这会将您的列表扩展为您可以同时查看所有子项的子项总数:)