Android notes: Nested listview methods in ScrollView _android

Source: Internet
Author: User
A few days ago because of the needs of the project, to put in a ListView in another ListView, that is, in a ListView each ListItem into another listview. But at the beginning, it will be found that the small ListView will show incomplete, its height has always been problematic. Check the Internet, found that other people have encountered such a problem, and most people do not recommend such a design, because by default, Android is prohibited in the ScrollView into the other ScrollView, its height is not calculated.

Search again, found that there are stackoverflow on the cattle have solved this problem, after the test found that can solve the problem, it is the idea is to set the ListView after the adapter, according to ListView sub-project recalculation listview height, The height is then set to the ListView as Layoutparams, so that its height is correct.
The following is the source code:
Copy Code code as follows:

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

Calling this static method after setting the ListView adapter allows ListView to appear correctly in the ListItem of its parent listview. Note, however, that each item of the child ListView must be linearlayout and cannot be otherwise, because other layout (such as relativelayout) do not rewrite onmeasure (), so the Onmeasure () Throws an exception when.

Another problem with nesting listview (or ScrollView) in ScrollView is that the child ScrollView cannot slide (if it does not show the full word) because the sliding event is eaten by the parent ScrollView, If you want to let the child ScrollView can also slide, can only forcibly intercept the slide event, there are cattle in the forum sent the code said can. Although I have not tried it myself, it is estimated to be feasible.

While the technical challenges of ScrollView in ScrollView can be compromised, such a design is a poor user experience because users are less likely to see and manipulate the contents of the child ScrollView. A good design, for example, is that each item of the parent ListView only shows a description of the generality, and then clicking on its item will go to another page to describe and display the item in detail.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.