ScrollView嵌套ListView显示不全的问题 -- 方法计算条目高度
/**
* scrollview嵌套listview显示不全解决
*
* @param listView
*/
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);
}
ScrollView嵌套ListView 或者GridView -- 扩展ListView或者GridView
/**
* 解决ScollView嵌套ListView 内容显示不全
*/
public class ScollViewListView extends ListView {
public ScollViewListView(Context context) {
super(context);
}
public ScollViewListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScollViewListView(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);
}
}