本文共 911 字,大约阅读时间需要 3 分钟。
动态设定GridView的高度,固定column,根据gridview中的item个数设定高度:
调用以下方法:
- public static void setListViewHeightBasedOnChildren(GridView listView) {
-
- ListAdapter listAdapter = listView.getAdapter();
- if (listAdapter == null) {
- return;
- }
-
- int col = 4;
- int totalHeight = 0;
-
-
- for (int i = 0; i < listAdapter.getCount(); i += col) {
-
- View listItem = listAdapter.getView(i, null, listView);
- listItem.measure(0, 0);
-
- totalHeight += listItem.getMeasuredHeight();
- }
-
-
- ViewGroup.LayoutParams params = listView.getLayoutParams();
-
- params.height = totalHeight;
-
- ((MarginLayoutParams) params).setMargins(10, 10, 10, 10);
-
- listView.setLayoutParams(params);
- }
调用此方法后,需要在调用notifyDataSetChanged()方法,实现界面刷新 转载地址:http://fopnn.baihongyu.com/