In android, scrollview contains the height settings of listView.

Source: Internet
Author: User

If the scrollview contains a listView, a sliding problem occurs .. The most common problem is that listview cannot slide... The cause is that the height of the listview should be determined according to the data bound by the adapter when it is loaded, rather than fill_parent or wrap_content...

If the project is used, extract it and make a record.

Import android. view. view; import android. view. viewGroup; import android. widget. listAdapter; import android. widget. listView; public class ListViewUtils {public void setListViewHeightBasedOnChildren (ListView listView) {// obtain the Adapter ListAdapter listAdapter = ListView corresponding to listView. getAdapter (); if (listAdapter = null) {return;} int totalHeight = 0; for (int I = 0, len = listAdapter. getCount (); I <len; I ++) {// listAdapter. getCount () returns the number of data items. View listItem = listAdapter. getView (I, null, listView); listItem. measure (0, 0); // calculate the width and height of the subitem View totalHeight + = listItem. getMeasuredHeight (); // calculate the total height of all subitems} ViewGroup. layoutParams params = listView. getLayoutParams (); params. height = totalHeight + (listView. getDividerHeight () * (listAdapter. getCount ()-1); // listView. getDividerHeight () obtains the height occupied by separators between subitems. // params. the height finally shows the required height for the entire ListView. setLayoutParams (params );}}
The specific usage is to use this util to dynamically determine the height after the listview binds the data adapter.

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.