Created
August 1, 2015 03:37
-
-
Save atearsan/86c9209c1f11be2e60b9 to your computer and use it in GitHub Desktop.
scrollview 嵌套 listview 导致listview显示不完整解决方式
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 重新计算listview高度<br/> | |
| * <i> | |
| * scrollview 嵌套 listview, 会导致listview只显示一个, 所以在listview.setAdapter之后重新计算高度<br/> | |
| * listview 必须是linearlayout布局 | |
| * </i> | |
| * | |
| * @param listView listview | |
| */ | |
| public void setListViewHeightBasedOnChildren(ListView listView) { | |
| ListAdapter listAdapter = listView.getAdapter(); | |
| if (listAdapter == null) { | |
| // pre-condition | |
| return; | |
| } | |
| 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(); | |
| } | |
| ViewGroup.LayoutParams params = listView.getLayoutParams(); | |
| params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); | |
| listView.setLayoutParams(params); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment