自定义流布局FlowGroup

package com.customview.customviewgroup;

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

/**
 * Created by changfeng on 2017/5/18.
 */

public  class FlowGroup extends ViewGroup{
    public FlowGroup(Context context) {
        super(context);
    }

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

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

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

        int xPoint=0;
        int yPoint=0;

        int rowHeightMax=0;//行高,根据最高的childView的高确定
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            int measuredWidth = childView.getMeasuredWidth();
            int measuredHeight = childView.getMeasuredHeight();



            MarginLayoutParams layoutParams = (MarginLayoutParams) childView.getLayoutParams();

            int widthTotal = measuredWidth+layoutParams.leftMargin+layoutParams.rightMargin;
            int heightTotal = measuredHeight+layoutParams.topMargin+ layoutParams.bottomMargin;
            rowHeightMax = Math.max(rowHeightMax,heightTotal);
            //当前位置+子view宽度对比parentView宽度,
            if((xPoint+widthTotal)>r) {
                xPoint = 0;
                yPoint=yPoint+rowHeightMax; //高度换行
                rowHeightMax = heightTotal;
            }
            int l1,t1,r1,b1;
            l1 = xPoint+layoutParams.leftMargin;
            t1 = yPoint+layoutParams.topMargin;
            r1 = l1+measuredWidth;
            b1 = t1+measuredHeight;
            childView.layout(l1,t1,r1,b1);

            xPoint = xPoint+widthTotal;
        }
    }

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

        int widthFlowGroup = 0;
        int heightFlowGroup = 0;

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode= MeasureSpec.getMode(heightMeasureSpec);
        int heightSize= MeasureSpec.getSize(heightMeasureSpec);

        //计算childView
        measureChildren(widthMeasureSpec,heightMeasureSpec);
//        计算宽度
        if(widthMode==MeasureSpec.EXACTLY){
            //精确的值,为match_parent或者具体数据
            widthFlowGroup = widthSize;
        }else{
            int childCount = getChildCount();
            int widthChildTotal = 0;
            for (int i = 0; i < childCount; i++) {
                View childView = getChildAt(i);
                int widthChild = childView.getMeasuredWidth();
                int childPaddingLeft = childView.getPaddingLeft();
                int childPaddingRight= childView.getPaddingRight();
                ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) childView.getLayoutParams();
                widthChildTotal=widthChildTotal+
                        widthChild+
                        childPaddingLeft+childPaddingRight+
                        layoutParams.leftMargin+layoutParams.rightMargin;
            }
            widthFlowGroup = widthChildTotal;
        }
//        计算宽度
        if(heightMode==MeasureSpec.EXACTLY){
            //精确的值,为match_parent或者具体数据
            heightFlowGroup = heightSize;
        }else{
            int childCount = getChildCount();
            int heightChildTotal = 0;
            for (int i = 0; i < childCount; i++) {
                View childView = getChildAt(i);
                int heightChild = childView.getMeasuredHeight();
                int childPaddingTop = childView.getPaddingTop();
                int childPaddingBottom= childView.getPaddingBottom();
                ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) childView.getLayoutParams();
                heightChildTotal=heightChildTotal+
                        heightChild+
                        childPaddingTop+childPaddingBottom+
                        layoutParams.topMargin+layoutParams.topMargin;
            }
            heightFlowGroup = heightChildTotal;
        }

        setMeasuredDimension(widthFlowGroup,heightFlowGroup);
    }

    @Override
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
    {
        return new MarginLayoutParams(getContext(), attrs);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值