流式布局

概念

        根据要添加的子view的宽判断是否要换行。常用来显示热门标签等。

参考

        http://www.imooc.com/video/5145(慕课网视频)

思路

        需要自定义LayoutParams,在使用时可以定义标签的margin,故可使用系统的MarginLayoutParams。通过比较当前行已经有宽度与新添加的view的宽度的和,与组件本身的宽度的大小,从而决定新组件是不是要换行。

代码

public class FlowLayout extends ViewGroup {

	public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

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

	public FlowLayout(Context context) {
		super(context);
	}

	@Override
	protected LayoutParams generateDefaultLayoutParams() {
		return new MarginLayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
	}

	@Override
	protected boolean checkLayoutParams(LayoutParams p) {
		return p instanceof MarginLayoutParams;
	}

	@Override
	public LayoutParams generateLayoutParams(AttributeSet attrs) {
		return new MarginLayoutParams(getContext(), attrs);
	}

	@Override
	protected LayoutParams generateLayoutParams(LayoutParams p) {
		return new MarginLayoutParams(p);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);
		int width = getPaddingLeft() + getPaddingRight();
		int height = getPaddingTop() + getPaddingBottom();
		int curWidth = getPaddingLeft() + getPaddingRight();// 当前行的宽高
		int curHeight = 0;
		for (int x = 0; x < getChildCount(); x++) {
			View view = getChildAt(x);
			measureChild(view, widthMeasureSpec, heightMeasureSpec);
			MarginLayoutParams lp = (MarginLayoutParams) view.getLayoutParams();
			int childWidth = view.getMeasuredWidth() + lp.leftMargin
					+ lp.rightMargin;
			int childHeight = view.getMeasuredHeight() + lp.topMargin
					+ lp.bottomMargin;
			if (curWidth + childWidth > widthSize) {// 换行
				width = Math.max(width, curWidth);// 获取已测量过的行的最大宽度
				height += curHeight;// 换行后,总高度要加上最后一行的高度
				curWidth = childWidth + getPaddingLeft() + getPaddingRight();// 新行的当前宽度为新添加到该行的子view的宽度
				curHeight = childHeight;// 换到新行时,新行的高度等于新添加的view的高度
			} else {
				curWidth += childWidth;// 不用换行的话,就将该子view的宽度添加到该行的宽度中
				curHeight = Math.max(curHeight, childHeight);// 没换行时,更新当前行的高度(有可能新添加的view比原来的高)
			}
			if (x == getChildCount() - 1) {// 到最后一个子view时
				height += curHeight;// 该view所在的行的高度并没有加到height中
				width = Math.max(width, curWidth);
			}
		}
		if(getChildCount() == 0){
			width = 0;
			height = 0;
		}
		setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize
				: width, heightMode == MeasureSpec.EXACTLY ? heightSize
				: height);
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		int widthSize = getMeasuredWidth();
		int width = getPaddingLeft() + getPaddingRight();
		int height = getPaddingTop() + getPaddingBottom();
		int curWidth = getPaddingLeft() + getPaddingRight();// 当前行的宽高
		int curHeight = 0;
		for (int x = 0; x < getChildCount(); x++) {
			View view = getChildAt(x);
			if(view.getVisibility() == GONE)
				continue;
			MarginLayoutParams lp = (MarginLayoutParams) view.getLayoutParams();
			int childWidth = view.getMeasuredWidth() + lp.leftMargin
					+ lp.rightMargin;
			int childHeight = view.getMeasuredHeight() + lp.topMargin
					+ lp.bottomMargin;
			if (curWidth + childWidth > widthSize) {// 需要进行换行
				width = Math.max(width, curWidth);// 获取已测量过的行的最大宽度
				height += curHeight;// 换行后,总高度要加上最后一行的高度,但并没有加上新换的行的高度
				curWidth = childWidth + getPaddingLeft() + getPaddingRight();// 新行的当前宽度为新添加到该行的子view的宽度
				curHeight = childHeight;// 换到新行时,新行的高度等于新添加的view的高度
				int left = getPaddingLeft() + lp.leftMargin;
				int top = height + lp.topMargin - getPaddingBottom();
				view.layout(left, top, left + view.getMeasuredWidth(), top
						+ view.getMeasuredHeight());
			} else {
				curHeight = Math.max(curHeight, childHeight);// 没换行时,更新当前行的高度(有可能新添加的view比原来的高)
				int l1;
				if (x == 0) {
					l1 = getPaddingLeft() + lp.leftMargin;
				} else {
					l1 = curWidth + lp.leftMargin;
				}
				int t1 = height + lp.topMargin - getPaddingBottom();
				view.layout(l1, t1, l1 + view.getMeasuredWidth(),
						t1 + view.getMeasuredHeight());
				curWidth += childWidth;// 不用换行的话,就将该子view的宽度添加到该行的宽度中
			}
		}
	}
}

注释

        具体注释见代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值