如何强制ListView的高度是尺寸,使每一行是可见的? [英] How to force height of listView to be size so that every row is visible?
本文介绍了如何强制ListView的高度是尺寸,使每一行是可见的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有嵌套的ListView。如何强制ListView的高度是尺寸,使每一行是可见的?
ListView控件嵌套在滚动型所以滚动不适合的ListView工作,所以我需要让所有的物品都在里面的ListView这是嵌套离子滚动型可见(上面有波纹管和更多的东西)。
I have nested listView. How to force height of listView to be size so that every row is visible ? ListView is nested inside ScrollView so scroll doesn't work for ListView so I need to make that all items are visible inside ListView which is nested ion ScrollView ( there is above and bellow more things).
推荐答案
要计算的ListView hieght
To calculate hieght of listView
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),
MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
通列表视图对象这一梅索德。
Pass listview object to this methode.
setListViewHeightBasedOnChildren(listView)
这篇关于如何强制ListView的高度是尺寸,使每一行是可见的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文