展会信息港展会大全

在scrollview中嵌套listview使高度正常显示
来源:互联网   发布日期:2016-01-14 12:09:58   浏览:2174次  

导读:public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) return; if (listAdapter.getCount() = 1) return; int desiredWidth = MeasureSpec.makeMeasureSp...

public static void setListViewHeightBasedOnChildren(ListView listView) {

ListAdapter listAdapter = listView.getAdapter();

if (listAdapter == null)

return;

if (listAdapter.getCount() <= 1)

return;

int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),

MeasureSpec.AT_MOST);

int totalHeight = 0;

View view = null;

for (int i = 0; i < listAdapter.getCount(); i++) {

view = listAdapter.getView(i, view, listView);

view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);

totalHeight += view.getMeasuredHeight();

System.out.println(totalHeight + "totalheight");

}

ViewGroup.LayoutParams params = listView.getLayoutParams();

params.height = totalHeight

+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));

listView.setLayoutParams(params);

listView.requestLayout();

}

赞助本站

人工智能实验室

相关热词: android开发 android教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港