最大高度的listview

public class DynamicHeightListView extends ListView {


	private int maxHeight = BdUtilHelper.getDimens(getContext(), R.dimen.ds400);
	
	public DynamicHeightListView(Context context) {
		super(context);
	}


	public DynamicHeightListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}


	public DynamicHeightListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}


	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		Log.i("ooo", "onMeasure.....");
	}


	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		Log.i("ooo", "onLayout.....");
		setViewHeightBasedOnChildren();
	}


	public void setViewHeightBasedOnChildren() {
		ListAdapter listAdapter = this.getAdapter();
		if (listAdapter == null) {
			return;
		}
		// int h = 10;
		// int itemHeight = BdUtilHelper.getDimens(getContext(), R.dimen.ds30);
		int sumHeight = 0;
		int size = listAdapter.getCount();


		for (int i = 0; i < size; i++) {
			View v = listAdapter.getView(i, null, this);
			v.measure(0, 0);
			Log.i("melon", "item.heiht = " + v.getMeasuredHeight());
			sumHeight += v.getMeasuredHeight();


		}


		if (sumHeight > maxHeight) {
			sumHeight = maxHeight;
		}
		android.view.ViewGroup.LayoutParams params = this.getLayoutParams();
		// this.getLayoutParams();
		params.height = sumHeight;


		this.setLayoutParams(params);
	}


	public int getMaxHeight() {
		return maxHeight;
	}


	public void setMaxHeight(int maxHeight) {
		this.maxHeight = maxHeight;
	}


}

你可能感兴趣的:(Android开发)