Practical Android skills: How to nest listview in scrollview

Source: Internet
Author: User

A few days ago, due to project requirements, another ListView should be placed in one ListView, that is, another ListView should be placed in each ListItem of one ListView. However, at the beginning, we will find that the placed small ListView is not completely displayed, and its height is always faulty. I checked it online and found that someone else encountered this problem. Most people do not recommend this design, because by default, Android prohibits the placement of another ScrollView IN THE ScrollView, its Height cannot be calculated.

I searched again and found that the cool man on StackOverflow has solved this problem. After the experiment, I found that the problem can be solved. The idea is that after configuring the ListView Adapter, re-calculate the height of the ListView based on the sub-item of the ListView, and then set the height to the ListView as the LayoutParams, so that the height is correct. The following is the source code:

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

You only need to call this static method after setting the ListView Adapter so that the ListView is correctly displayed in the ListItem of its parent ListView. However, it should be noted that each Item in the sublistview must be LinearLayout, but not others, because other Layout (such as RelativeLayout) does not overwrite onMeasure (), so it will be in onMeasure () throw an exception.

Another problem of nesting ListView (or ScrollView) in ScrollView is that the subscrollview cannot be slide (if it is not completely displayed) because the slide event will be eaten by the parent ScrollView, if you want to slide the subscrollview, you can only forcibly intercept the slide event. some cool people have posted code in the Forum to say yes. Although I have not tried it myself, it is estimated that it is feasible.

Although scrollview shows that the technical difficulties of scrollview can be broken, this design is a very poor user experience because users will not easily see and operate the content in the subscrollview. For example, a good design is that each item of the parent listview only displays a general description, and then click its item to go to another page to describe and display in detail and perform operations on this item.

References: http://stackoverflow.com/questions/3495890/how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing

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.