Solve the Problem of incomplete display of ScollView nested ListView in Android Development

Source: Internet
Author: User

Solve the Problem of incomplete display of ScollView nested ListView in Android Development

Most of the time, we do not need to set a ScollView on the outside of the ListView when using ListView to load data, because the ListView itself can scroll to display data. Sometimes we need to display other data in addition to the List data in ListView. In this case, we need to set an Scollview on the most layer of the entire page, otherwise, the display may be faulty (for example, if there is already a lot of other data on the ListView, the display on the mobile phone will directly cause the ListView to be invisible, then the ScollView will be added throughout the screen layout to achieve the sliding interface ), anyone who has used ScollView nested ListView knows that, without task processing, the data of ListView can only display a few more lines, but nothing else can be seen. How can this problem be solved? Generally, we can use the following two methods:

1. calculation height:

Private intTotalHeight = 0;

Public Static VoidSetListViewHeight (ListView listView ){

/* Get the adapter */

Aadpter adapter = listView. getAdapter ();

/* Traverse the control */

For(IntI = 0; I <adapter. getCount (); I ++ ){

View view = adapter. getView (I,Null, ListView );

/* Measure the height of the control at once */

View. measure (View. MeasureSpec.UNSPECIFIED, View. MeasureSpec.UNSPECIFIED);

TotalHeight+ = View. getMeasuredHeight ();

}

/* Gap between controls */

TotalHeight+ = ListView. getDividerHeight () * (listView. getCount ()-1 );

/* 2. assign a value to the LayoutParams object of ListView */

ViewGroup. LayoutParams params = listView. getLayoutParams ();

Params. height =TotalHeight;

ListView. setLayoutParams (params );

}

2. Override the onMeasure of ListView:

Public class WholeListView extends ListView {

Public WholeListView (Context context ){
Super (context );
}

Public WholeListView (Context context, AttributeSet attrs ){
Super (context, attrs );
}

Public WholeListView (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
}

@ Override
Protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec ){
Int expandSpec = MeasureSpec. makeMeasureSpec (Integer. MAX_VALUE> 2,
MeasureSpec. AT_MOST );
Super. onMeasure (widthMeasureSpec, expandSpec );
}
}

Of course, the above two methods are also effective for the GridView and Expandlistview.

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.