您的位置:首页 > 其它

ListView嵌套在ScrollView内显示不全的解决办法

2013-07-02 20:15 447 查看
ListView嵌套在ScrollView里面,只能够显示第一条数据。

因为ListView其实也是一个scrollView,scrollView默认是禁止嵌套的,起码导致内部的scrollView无法正确计算高度。[为什么会无法正常计算?]

解决办法也很简单,就是手动计算listView的高度并动态设置。

public static void setListViewHeightBaseOnChildren(ListView listView) {
if (listView != null) {
ListAdapter adapter = listView.getAdapter();
int totalHeight = 0;
int count = adapter.getCount();
for (int i = 0; i < count; i++) {
           //View child = listView.getChildAt(i);//crash[why?]
View child = adapter.getView(i, null, listView);
child.measure(0, 0);
totalHeight += child.getMeasuredHeight();//有关获取一个view高度的方法?
}
if (count > 0) {
totalHeight += (count - 1) * listView.getDividerHeight();
}
LayoutParams params = listView.getLayoutParams();
params.height = totalHeight;
listView.setLayoutParams(params);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐