在ScrollView中嵌套ListView时,需要根据内容来设置listView的高度

动态设置ListView的高度

public void setListViewHeightBasedOnChildren(ListView mListView) {
		ListAdapter listAdapter = mListView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, mListView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = mListView.getLayoutParams();
		params.height = totalHeight + (mListView.getDividerHeight() * (listAdapter.getCount() - 1));
		((MarginLayoutParams) params).setMargins(10, 10, 10, 10);
		mListView.setLayoutParams(params);
	}

解决ScrollView嵌套ListView只显示一行的问题

你可能感兴趣的:(在ScrollView中嵌套ListView时,需要根据内容来设置listView的高度)