重新计算Listview高度

来源:互联网 发布:挪威船级社软件 招聘 编辑:程序博客网 时间:2024/05/04 23:50
public static synchronized 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(0, 0);totalHeight += listItem.getMeasuredHeight();}ViewGroup.LayoutParams params = listView.getLayoutParams();params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));listView.setLayoutParams(params);}

2、

public class NoScrollListView extends ListView{        public NoScrollListView(Context context, AttributeSet attrs){           super(context, attrs);      }       public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){           int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);           super.onMeasure(widthMeasureSpec, mExpandSpec);      }  }  


0 0
原创粉丝点击