Android getChildMeasureSpec的实例代码讲解

流式布局项目

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FlowlayoutActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">
        <com.lgj.xxkt.flowlayout.Flayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shape_button_circular"
                android:text="惠氏3段" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shape_button_circular"
                android:text="奶粉2段" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shape_button_circular"
                android:text="图书勋章日" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shape_button_circular"
                android:text="伯爵茶" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shape_button_circular"
                android:text="阿迪5折秒杀" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shape_button_circular"
                android:text="蓝胖子" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shape_button_circular"
                android:text="婴儿洗衣机" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shape_button_circular"
                android:text="小度在家" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shape_button_circular"
                android:text="遥控车可坐" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shape_button_circular"
                android:text="搬家袋" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shape_button_circular"
                android:text="剪刀车" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shape_button_circular"
                android:text="滑板车儿童" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shape_button_circular"
                android:text="空调风扇" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shape_button_circular"
                android:text="空鼓锤" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/shape_button_circular"
                android:text="笔记本电脑" />
        </com.lgj.xxkt.flowlayout.Flayout>
    </LinearLayout>

</ScrollView>

终点一个View 的MeasureSpec计算规则如下图所示:
在这里插入图片描述

package com.lgj.xxkt.flowlayout;

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

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

public class Flayout extends ViewGroup {
    private int mHorizontalSpacing = 15;//每个item横向间距
    private int mVerticalSpacing = 15; //每个item横向间距
    private List<List<View>> allLines = new ArrayList<>(); // 记录所有的行,一行一行的存储,用于layout
    List<Integer> lineHeights = new ArrayList<>(); // 记录每一行的行高,用于layout

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

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

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

    //自己测量  widthMeasureSpec 和 heightMeasureSpec 是来自Flayout的父亲
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        initMeasureParams();
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();

        int selfWidth = MeasureSpec.getSize(widthMeasureSpec);// 解析父亲给我的宽度
        int selfHeight = MeasureSpec.getSize(heightMeasureSpec);// 解析父亲给我的高度

        int parentNeededWidth = 0;//measure 过程中,子view要求父ViewGroup的宽
        int parentNeededHeight = 0;//measure 过程中,子view要求父ViewGroup的高

        ArrayList<View> lineViews = new ArrayList<>();//保存一行中的所有View
        int lineWidthUsed = 0;//记录这行已经使用了多宽的size
        int lineHeight = 0;

        // 一个是自己,一个是孩子,设计的话,先度量自己还是孩子,ViewPager先自己再孩子
        // 绝大部分都是先孩子然后自己,一般情况下都是所有的孩子的宽、高、加起来就是自己的大小
        int childCount = getChildCount();

        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            // LayoutParams 获取的是孩子自身的布局参数,也就是下面的这个值
            // android:layout_width="wrap_content"
            //  android:layout_height="wrap_content"
            LayoutParams layoutParams = childView.getLayoutParams();
            // View的计算是递归算法,
            // MeasureSpec: mode + size 组成,高2位表示模式,低30位表示大小
            // 模式有: EXACTLY  具体的大小或者 Match_Parent
            //        AT_MOST  Warp_parent
            //        UNSPECIFIED  ListView
            // 将LayoutParams转化成具体的 measureSpec,
            // 这个算法的规则是,根据父亲的模式以及自身的LayoutParams决定(也就是一个View的大小由自身的LayoutParams和父View决定)
            // 第一个参数是父亲的MeasureSpec,第二个参数:Padding是父亲的,第三个参数:
            int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, paddingLeft + paddingRight, layoutParams.width);
            int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, paddingTop + paddingBottom, layoutParams.height);

            // 斗量子view的方法,就把孩子斗量完了
            childView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
            int measuredWidth = childView.getMeasuredWidth();
            int measuredHeight = childView.getMeasuredHeight();
            // 这里需要换行,等于说 接下来要放置的控件放不下了,需要换行
            if (lineWidthUsed + measuredWidth + mHorizontalSpacing > selfWidth) {
                allLines.add(lineViews);
                lineHeights.add(lineHeight);

                // 一旦换行,我们就可以判断当前需要的宽和高了,所以要记录起来
                parentNeededHeight = lineHeight + parentNeededHeight + mVerticalSpacing;
                parentNeededWidth = Math.max(parentNeededWidth, lineWidthUsed + mHorizontalSpacing);

                lineViews = new ArrayList<>();
                lineWidthUsed = 0;
                lineHeight = 0;
            }

            lineViews.add(childView);
            // 每行也需要加上空格
            lineWidthUsed = lineWidthUsed + measuredWidth + mHorizontalSpacing;
            // 获取每行最高的高度
            lineHeight = Math.max(lineHeight, measuredHeight);

            //处理最后一行数据
            if (i == childCount -1){
                allLines.add(lineViews);
                lineHeights.add(lineHeight);
                // 一旦换行,我们就可以判断当前需要的宽和高了,所以要记录起来
                parentNeededHeight = lineHeight + parentNeededHeight + mVerticalSpacing;
                parentNeededWidth = Math.max(parentNeededWidth, lineWidthUsed + mHorizontalSpacing);
            }

        }

        // setMeasuredDimension 此接口是设置自己的大小,并且保存起来
        // 在度量自己,并保存,父亲想要获取的时候,直接调用孩子的 child.getMeasureWidth就行
        //setMeasuredDimension(width,height);


        // 根据子View的度量结果,来重新度量自己ViewGroup
        // 作为一个ViewGroup,它自己也是一个view,它的大小也是需要根据他父亲给他提供的宽高来度量
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int realWidth = widthMode == MeasureSpec.EXACTLY ? selfWidth : parentNeededWidth;
        int realHeight = heightMode == MeasureSpec.EXACTLY ? selfHeight : parentNeededHeight;
        // 这个传递的是具体的size,不是MeasureSpec
        setMeasuredDimension(realWidth, realHeight);
    }

    private void initMeasureParams() {
        allLines.clear();
        lineHeights.clear();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // 总共的行数
        int lineCount = allLines.size();
        int curL = getPaddingLeft();
        int curT = getPaddingTop();
        for (int i = 0; i < lineCount; i++) {//
            List<View> lineViews = allLines.get(i);
            int lineHeight = lineHeights.get(i);
            for (int j = 0; j < lineViews.size(); j++) {// 每行的view进行布局
                View view = lineViews.get(j);
                int left = curL;
                int top = curT;
                int right = left + view.getMeasuredWidth();
                int bottom = top + view.getMeasuredHeight();
                view.layout(left, top, right, bottom);
                // 是你调用完这个View的layout之后才会有的值,而getMeasuredWidth 是你调用完measure之后就可以获取
                curL = right + mHorizontalSpacing;
            }
            curL = getPaddingLeft();
            curT = curT + lineHeight + mVerticalSpacing;
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值