解决方法1:
public void initListViewBaseChildren(ListView lv, BaseAdapter adapter) {
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = lv.getLayoutParams();
params.height = totalHeight
+ (lv.getDividerHeight() * (adapter.getCount() - 1));
lv.setLayoutParams(params);
}
解决方法2:
自定义的ListView,重写onMeasure
public class BusPreferenceListView extends ListView {
public BusPreferenceListView(Context context) {
super(context);
}
public BusPreferenceListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public BusPreferenceListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
注:到目前为止,还没有弄懂为什么listview只加载一条,求大神指点,留言!!!谢谢!!!!!!