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, 這個裏邊有很多要注意的細節。