我正在开发一个应用程序,我必须在scrollview中显示listview,问题是我没有得到listview的正确高度,我已经跟随了几乎所有来自下面链接的代码。
Android ListView rows in ScrollView not fully displayed - clipped
How can I put a ListView into a ScrollView without it collapsing?
我不知道如何计算列表的高度我尝试了不同的方法,但由于列表中每行的可变大小,它不起作用。如果有人知道如何计算它在适配器文件中运行时间或从主活动/片段中告诉我应该怎么做。
请帮助我,并提前致谢。
答案 0 :(得分:0)
简单的解决方案。添加此行setListViewHeightBasedOnChildren(item_Listview);
和下面的方法
if(item_lists != null && item_lists.size() > 0) {
itl = new Itemlist(getActivity(), item_lists);
item_Listview.setAdapter(itl);
setListViewHeightBasedOnChildren(item_Listview);
}
private void setListViewHeightBasedOnChildren(ListView item_listview) {
ListAdapter listAdapter = item_listview.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, item_listview);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = item_listview.getLayoutParams();
params.height = totalHeight
+ (item_listview.getDividerHeight() * (listAdapter.getCount() - 1));
item_listview.setLayoutParams(params);
}