Android 流式布局

3.重写 onMeasure() 和 onLayout() 来全新定制自定义 ViewGroup 的内部布局,这种方式不单单需要测量本身自己的宽高,还要根据需要计算每个子view的位置,稍微复杂一些,本次的流式布局就是通过这种方式实现的。

简单讲解一下整个过程:

大体上分为测量尺寸过程以及布局过程

测量尺寸过程:ViewGroup 的mesure()方法被父View调用,进而调用到 onMeasure() ,在onMeasuse()中会调用所有子 View 的 measure() 让它们进行自我测量,并根据子 View 计算出的希望的尺寸来计算出它们的实际尺寸和位置(注意是希望的尺寸,不是最终尺寸,比如ViewGroup 的宽为100dp,有一个子View的宽度为200dp,显然子View最多也只能是100dp的宽度)然后保存。同时,它也会根据子 View 的尺寸和位置来计算出自己的尺寸然后保存,到这里我们就确定了ViewGroup自身的希望尺寸,以及它的子View的希望尺寸,那么,具体字View怎么摆放呢?这就到了下一个阶段,布局阶段。

布局过程:VIewGrouplayout() 方法被父 View 调用,在 layout() 中它会取得父 View 传进来的自己的位置和尺寸,并且调用 onLayout() 来进行实际的内部布局ViewGroup 在 onLayout() 中会调用自己的所有子 View 的 layout() 方法,把它们的尺寸和位置传给它们,让子View完成布局。

代码:

import android.content.Context;

import android.content.res.TypedArray;

import android.util.AttributeSet;

import android.view.View;

import android.view.ViewGroup;

import java.util.HashMap;

import java.util.Map;

/**

  • @SharksLee lishaojie

*/

public class XFlowLayout extends ViewGroup {

private int childHorizontalSpace;

private int childVerticalSpace;

private Map<Object, Location> mLocationMap;

public XFlowLayout(Context context, AttributeSet attrs) {

super(context, attrs);

TypedArray attrArray = context.obtainStyledAttributes(attrs, R.styleable.XLFlowLayout);

mLocationMap = new HashMap<>();

if (attrArray != null) {

childHorizontalSpace = attrArray.getDimensionPixelSize(R.styleable.XLFlowLayout_childHorizontalSpace, 0);

childVerticalSpace = attrArray.getDimensionPixelSize(R.styleable.XLFlowLayout_childVerticalSpace, 0);

attrArray.recycle();

}

}

@Override

protected LayoutParams generateLayoutParams(LayoutParams p) {

return new MarginLayoutParams§;

}

@Override

public LayoutParams generateLayoutParams(AttributeSet attrs) {

return new MarginLayoutParams(getContext(), attrs);

}

@Override

protected LayoutParams generateDefaultLayoutParams() {

return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

}

/**

  • 负责设置子控件的测量模式和大小 根据所有子控件设置自己的宽和高

*/

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

if (mLocationMap != null && !mLocationMap.isEmpty()) {

mLocationMap.clear();

}

// 获得它的父容外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

资料获取→专栏
器为它设置的测量模式和大小

int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);

int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);

int modeWidth = MeasureSpec.getMode(widthMeasureSpec);

int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

// 如果是warp_content情况下,记录宽和高

int width = 0;

int height = 0;

/**

  • 记录每一行的宽度,width不断取最大宽度

*/

int lineWidth = 0;

/**

  • 每一行的高度,累加至height

*/

int lineHeight = 0;

int count = getChildCount();

int left = getPaddingLeft();

int top = getPaddingTop();

// 遍历每个子元素

for (int i = 0; i < count; i++) {

View child = getChildAt(i);

if (child.getVisibility() == GONE)

continue;

// 测量每一个child的宽和高

measureChild(child, widthMeasureSpec, heightMeasureSpec);

// 得到child的lp

LayoutParams lp = child.getLayoutParams();

// 当前子空间实际占据的宽度

int childWidth = child.getMeasuredWidth() + childHorizontalSpace;

// 当前子空间实际占据的高度

int childHeight = child.getMeasuredHeight() + childVerticalSpace;

if (lp != null && lp instanceof MarginLayoutParams) {

MarginLayoutParams params = (MarginLayoutParams) lp;

childWidth += params.leftMargin + params.rightMargin;

childHeight += params.topMargin + params.bottomMargin;

}

/**

  • 如果加入当前child的宽度,超出了最大宽度,则的到目前最大宽度给width,累加height 然后开启新行

*/

if (lineWidth + childWidth > sizeWidth - getPaddingLeft() - getPaddingRight()) {

width = Math.max(lineWidth, childWidth);// 取最大的

lineWidth = childWidth; // 重新开启新行,开始记录

// 叠加当前高度,

height += lineHeight;

// 开启记录下一行的高度

lineHeight = childHeight;

mLocationMap.put(child, new Location(left, top + height, childWidth + left - childHorizontalSpace, height +

child.getMeasuredHeight() + top));

} else {// 否则累加值lineWidth,lineHeight取最大高度

mLocatio
nMap.put(child, new Location(lineWidth + left, top + height, lineWidth + childWidth -

childHorizontalSpace + left, height + child.getMeasuredHeight() + top));

lineWidth += childWidth;

lineHeight = Math.max(lineHeight, childHeight);

}

}

width = Math.max(width, lineWidth) + getPaddingLeft() + getPaddingRight();

height += lineHeight;

sizeHeight += getPaddingTop() + getPaddingBottom();

height += getPaddingTop() + getPaddingBottom();

setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth : width, (modeHeight == MeasureSpec.EXACTLY) ?

sizeHeight : height);

}

sizeHeight += getPaddingTop() + getPaddingBottom();

height += getPaddingTop() + getPaddingBottom();

setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth : width, (modeHeight == MeasureSpec.EXACTLY) ?

sizeHeight : height);

}

  • 22
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值