nested in ScrollView using the ListView, it appears that the ListView will only display a little more than one line and cannot slide. How to change the height of the ListView is problematic and does not conform to expectations. Search for some solutions, I think it is best not to use such a design, because by default, Android is forbidden to put another scrollview in the ScrollView, its height is not calculated.
Method one: After setting the adapter of the ListView, recalculate the height of the ListView according to the child items of the ListView, and then set the height again as layoutparams to the ListView, so that it is the correct height, the following is the source:
public class Utility {
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);
}
}
called after the Setadapter method
New Utility (). Setlistviewheightbasedonchildren (lv);
This static method is called after the adapter of the ListView is set so that the ListView is displayed correctly in the ListItem of its parent listview. Note, however, that each item of the child ListView must be linearlayout, not the other, because other layout (such as relativelayout) does not rewrite onmeasure (), so it will be in the Onmeasure () Throws an exception.
Another problem with nesting a ListView (or ScrollView) in ScrollView is that the child ScrollView cannot slide (if it is not shown), because the sliding event is eaten by the parent scrollview. If you want to let the sub-ScrollView can also slide, can only force interception of sliding events, there are cows in the forum sent code to say yes. Although I have not tried it myself, I think it is feasible.
While the technical challenges of ScrollView can be compromised in ScrollView, this design is a very poor user experience because it is not easy for users to see and manipulate the content in sub-scrollview. For example, a good design is that each item of the parent ListView only displays a general description, and then clicking on its item will go to another page to describe and display the item in detail.
Method Two:
The
Re-sets the height of the ListView in Java in order to "brace" the ListView:
Linearlayout.layoutparams lp5 =new Linearlayout.layoutparam (Layoutparams.fill_parent, Listitem.size () *51-1);
One of the first properties does not have to be said, The second one is to calculate the total height of the ListView to be set, 51 is the height of the row I set beforehand (50) + the interval between each row (1), listitem.size () is the number of rows I want to display, with. Setlayoutparams (LP5); To reset the height.