此代码有效,但问题是在ListView中,ListView的每个项目都是差异,因此不显示ListView的最后一项,因为大高度ListView项目获得此空间。
这是代码:
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的高度? 提前致谢..
答案 0 :(得分:1)
您的功能替换为
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();
}
答案 1 :(得分:0)
<强> ANS 强> 使用这个
mView.measure(MeasureSpec.makeMeasureSpec(listWidth,MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));
代码
mView.measure(MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED),MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));