美文网首页
自定义 ViewGroup - FlexboxLayout -

自定义 ViewGroup - FlexboxLayout -

作者: 熊er | 来源:发表于2017-07-27 10:46 被阅读0次

    效果

    微信图片_20170727104655.png

    实现

    /**
     * 弹性流式布局
     *
     * @author gavin.xiong 2017/7/26
     */
    public class FlexboxLayout extends ViewGroup {
    
        public FlexboxLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public LayoutParams generateLayoutParams(AttributeSet attrs) {
            return new MarginLayoutParams(getContext(), attrs);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    
            int width = 0, height = 0;
            int lineWidth = 0, lineHeight = 0;
    
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
    
                if (child.getVisibility() == GONE) {
                    continue;
                }
    
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
    
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
    
                int childWidth = lp.leftMargin + child.getMeasuredWidth() + lp.rightMargin;
                int childHeight = lp.topMargin + child.getMeasuredHeight() + lp.bottomMargin;
    
                if (lineWidth + childWidth > widthSize - getPaddingLeft() - getPaddingRight()) {
                    width = Math.max(width, lineWidth);
                    height += lineHeight;
                    lineWidth = childWidth;
                    lineHeight = childHeight;
                } else {
                    lineWidth += childWidth;
                    lineHeight = Math.max(lineHeight, childHeight);
                }
    
                // 如果是最后一行
                if (i == getChildCount() - 1) {
                    width = Math.max(width, lineWidth);
                    height += lineHeight;
                }
            }
    
            setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : width + getPaddingLeft() + getPaddingRight(),
                    heightMode == MeasureSpec.EXACTLY ? heightSize : height + getPaddingTop() + getPaddingBottom());
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            int left = getPaddingLeft();
            int top = getPaddingTop();
    
            int lineWidth = 0, lineHeight = 0;
    
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);
    
                if (child.getVisibility() == GONE) {
                    continue;
                }
    
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
    
                int childWidth = lp.leftMargin + child.getMeasuredWidth() + lp.rightMargin;
                int childHeight = lp.topMargin + child.getMeasuredHeight() + lp.bottomMargin;
    
                if (lineWidth + childWidth > getWidth() - getPaddingLeft() - getPaddingRight()) {
                    left = getPaddingLeft();
                    top += lineHeight;
    
                    lineWidth = childWidth;
                    lineHeight = childHeight;
                } else {
                    lineWidth += childWidth;
                    lineHeight = Math.max(lineHeight, childHeight);
                }
    
                int cl = left + lp.leftMargin;
                int ct = top + lp.topMargin;
                int cr = cl + child.getMeasuredWidth();
                int cb = ct + child.getMeasuredHeight();
                child.layout(cl, ct, cr, cb);
    
                left += lp.leftMargin + child.getMeasuredWidth() + lp.rightMargin;
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:自定义 ViewGroup - FlexboxLayout -

          本文链接:https://www.haomeiwen.com/subject/lefzkxtx.html