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.