hidden listview not showing all elements

你离开我真会死。 提交于 2019-12-10 11:55:06

问题


I have a layout in which there are some tags that opens and closes some extra information. One of these tags, shows a listview. But instead of showing all the elements, it shows just one by one with scrolling enabled. I just want to show all the items in the listview.

Normal state of the screen:

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

After clicking on the tag:

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

This is the critical xml of the screen:

        <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"/>

And this is the code of the adapter:

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:


Use this class as utility

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

and use this classs like this

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

this will expand your list to the total number of childs you can view all of them at same time :)



来源:https://stackoverflow.com/questions/17524319/hidden-listview-not-showing-all-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!