listview 嵌套listview 子listview不会把所有的 item 内容显示出来。
两种解决方法:
1,百度了很多博客内容,写了一个工具类,在子listview 设置完adapter后调用,代码如下:
新建类AdapterUtility:
public class AdapterUtility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) return;
int totalHeight = 0;
for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
}
2,重写子listview,这里命名为InListView。其他代码照搬,重要的地方是重写的onMeasure方法里边的内容。他会重新计算高度。
public class InListView extends ListView {
public InListView(Context context) {
super(context);
}
public InListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public InListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int newHeight=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, newHeight);
}
}
方法1参考博客:很多博客内容都是这样的,不知道谁是原创。
方法2参考博客:https://blog.csdn.net/qq_31296231/article/details/78929647, 这个里边有很多要注意的细节。