Android自定义流式布局

package com.ding.flowlayout;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class FlowLayout extends ViewGroup {

    private List> mAllView = new ArrayList<>();
    private List mLineHeight = new ArrayList<>();

    private static final int[] LL = new int[]{android.R.attr.maxLines};
    private int mMaxLines;

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

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

        TypedArray a = context.obtainStyledAttributes(attrs, LL);
        mMaxLines = a.getInt(0, Integer.MAX_VALUE);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        mAllView.clear();
        mLineHeight.clear();

        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);//容器的宽度

        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);//容器的高度
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);//高的模式


        int height = 0;//高的和
        int lineWidth = 0;//行宽
        int lineHeight = 0;//行高

        List lineViews = new ArrayList<>();
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() == GONE) {
                continue;
            }

            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

            int cWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            int cHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;

            if (lineWidth + cWidth > sizeWidth - (getPaddingLeft() + getPaddingRight())) {
                //当前行的宽+子view的宽>容器的宽,做换行处理
                height += lineHeight;

                mLineHeight.add(lineHeight);
                mAllView.add(lineViews);
                lineViews = new ArrayList<>();
                lineViews.add(child);

                //重置
                lineWidth = cWidth;
                lineHeight = cHeight;


            } else {
                //未换行
                lineWidth += cWidth;
                lineHeight = Math.max(lineHeight, cHeight);
                lineViews.add(child);

            }

            if (i == childCount - 1) {
                //最后一行
                height += lineHeight;
                mLineHeight.add(lineHeight);
                mAllView.add(lineViews);
            }
        }

        //maxLines 校正
        if (mMaxLines < mLineHeight.size()) {
            height = getMaxLinesHeight();
        }


        if (modeHeight == MeasureSpec.EXACTLY) {
            height = sizeHeight;
        } else if (modeHeight == MeasureSpec.AT_MOST) {
            height = Math.min(sizeHeight, height);
            height = height + getPaddingTop() + getPaddingBottom();
        } else if (modeHeight == MeasureSpec.UNSPECIFIED) {
            height = height + getPaddingTop() + getPaddingBottom();
        }

        setMeasuredDimension(sizeWidth, height);
    }

    private int getMaxLinesHeight() {
        int height = 0;
        for (int i = 0; i < mMaxLines; i++) {
            height += mLineHeight.get(i);
        }
        return height;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        int left = getPaddingLeft();
        int top = getPaddingTop();

        int lineNums = mAllView.size();
        for (int i = 0; i < lineNums; i++) {
            List lineViews = mAllView.get(i);
            int lineHeight = mLineHeight.get(i);
            for (int j = 0; j < lineViews.size(); j++) {
                View child = lineViews.get(j);
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
                //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 += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            }
            left = getPaddingLeft();
            top += lineHeight;

        }
    }

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

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

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

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

你可能感兴趣的:(android)