我需要在运行时获得准确的listView高度。
当我使用下面的代码时,每个listItem的高度都不正确。
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listview);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
params.height = totalHeight + listview.getDividerHeight()* (listAdapter.getCount() -1);
listview.setLayoutParams(params);
listview.requestLayout();
当我使用getChild版本时,Height准确但总计数已关闭......
int total = 0;
for (int i = 0; i < listview.getChildCount(); i++) {
View childAt = listview.getChildAt(i);
if (childAt == null)
continue;
int childH = childAt.getMeasuredHeight();
total += childH;
}
int div = listview.getDividerHeight();
total += (div * (listAdapter.getCount() - 1));
if (params.height != total) {
getLogger().info("setting measured buttons list to height: " + total);
params.height = total;
listview.requestLayout();
ViewParent parent = listview.getParent();
你遇到过这个问题吗?
答案 0 :(得分:8)
如果您只想找到listView
的高度,可以使用
listView.post(new Runnable()
{
public void run()
{
listView.getHeight();
}
});
答案 1 :(得分:2)
listItem.measure(0,0);度量0,0的参数使用第一个参数MeasureSpec.makeMeasureSpec(listWidth,MeasureSpec.EXACTLY)和第二个参数MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED))遵循此代码
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();
}
答案 2 :(得分:0)
使用它,我现在正在使用它并且它100%工作,我正在使用它来制作包含列表的滚动视图。
package com.emercs.android.easyihome.util;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
public class Helper {
public static void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
// do nothing return null
return;
}
// set listAdapter in loop for getting final size
int totalHeight = 0;
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
if (listItem != null) {
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
}
// setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
if (params != null) {
params.height = totalHeight
+ (myListView.getDividerHeight() * (myListAdapter
.getCount() - 1));
myListView.setLayoutParams(params);
// print height of adapter on log
}
}
}
检查并添加到listview
ScrollView scrollViewScenes = (ScrollView) findViewById(R.id.x);
if (scrollViewScenes != null) {
Helper.getListViewSize(listview);
}
希望这有帮助。