自定义流式布局FlexableLayot

在这里插入图片描述

/**
 * author : geyuecang
 * date   : 2019/4/23 15:15
 * desc   : desc
 */
public class FlexableLayout extends ViewGroup {
    public FlexableLayout(Context context) {
        this(context, null);
    }

    public FlexableLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //获取测量高度和测量模式
        int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
        int measureHeight = MeasureSpec.getSize(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int lineHeight = 0;// 记录行的高度
        int lineWidth = 0; // 记录每行的宽度
        int height = 0;    // 记录总高度
        int width = 0;     // 记录总宽度
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            measureChild(child, widthMeasureSpec, heightMeasureSpec);

            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;

            if (lineWidth + childWidth > measureWidth) {
                //当前行宽度加上当前child的宽度大于viewgroup的测量宽度,换行
                width = Math.max(width, lineWidth);
                height += lineHeight;

                //记录新一行的宽高
                lineWidth = childWidth;
                lineHeight = childHeight;
            } else {
                //不用换行
                lineWidth += childWidth;
                lineHeight = Math.max(lineHeight, childHeight);
            }
            //最后一行不会超出width,所以不会走if里面的代码,所以要特殊处理
            if (i == count - 1) {
                height += lineHeight;
                width = Math.max(width, lineWidth);
            }
        }

        setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? measureWidth : width,
                heightMode == MeasureSpec.EXACTLY ? measureHeight : height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int lineHeight = 0;
        int lineWidth = 0;
        int top = 0;
        int left = 0;

        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            if (lineWidth + childWidth <= getMeasuredWidth()) {
                //不换行
                lineHeight = Math.max(lineHeight, childHeight);
                lineWidth += childWidth;
            } else {
                //换行
                top += lineHeight;
                left = 0;

                lineHeight = childHeight;
                lineWidth = childWidth;
            }

            //计算child的l、t、r、b
            int lc = left + lp.leftMargin;
            int tc = top + lp.topMargin;
            int rc = lc + child.getMeasuredWidth();
            int bc = tc + child.getMeasuredHeight();
            child.layout(lc, tc, rc, bc);
            //累加left
            left += childWidth;
        }
    }
}

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

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

    @Override
    protected LayoutParams generateDefaultLayoutParams()
    {
        return new MarginLayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值