如何计算不同高度的ListItem ListView的高度在android系统? [英] how to calculate listview height of different height listitem in android?
本文介绍了如何计算不同高度的ListItem ListView的高度在android系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这code是工作,但问题是ListView控件ListView中的每个产品的ListView不显示,因为大的高度ListView项获得这个空间的差异让最后一个项目。
这里是code:
public static void setListViewHeightBasedOnChildren(ListView listView,DownloadAdapter adapter,LinearLayout layDownloads) {
ListAdapter mAdapter = adapter;
int totalHeight = 0,h=0;
int listWidth=listView.getMeasuredWidth();
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);
mView.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
h=mView.getMeasuredHeight();
totalHeight += mView.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (mAdapter.getCount() - 1))+h;
listView.setLayoutParams(params);
listView.requestLayout();
}
这里是屏幕
请帮我如何获得每个单元的列表视图的高度,并计算出的ListView heght?
在此先感谢..
推荐答案
你的函数由
公共静态无效getTotalHeightofListView(ListView控件ListView控件){
public static void getTotalHeightofListView(ListView listView) {
ListAdapter mAdapter = listView.getAdapter();
int totalHeight = 0;
int listWidth = listView.getMeasuredWidth();
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);
mView.measure(
MeasureSpec.makeMeasureSpec(listWidth, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
totalHeight += mView.getMeasuredHeight();
Log.w("HEIGHT" + i, String.valueOf(totalHeight));
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (mAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
这篇关于如何计算不同高度的ListItem ListView的高度在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文