package com.example.test;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class FlowLayout extends ViewGroup {
private int lineSpacing = 20;
public FlowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
int paddingTop = getPaddingTop();
int paddingBottom = getPaddingBottom();
int widthUsed = paddingLeft + paddingRight;
int heightUsed = paddingTop + paddingBottom;
int childMaxHeightOfThisLine = 0;
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
if (child.getVisibility() != GONE) {
int childUsedWidth = 0;
int childUsedHeight = 0;
measureChild(child, widthMeasureSpec, heightMeasureSpec);
childUsedWidth += child.getMeasuredWidth();
childUsedHeight += child.getMeasuredHeight();
FlowLayoutParams params = (FlowLayoutParams) child
.getLayoutParams();
childUsedWidth += params.leftMargin + params.rightMargin;
childUsedHeight += params.topMargin + params.bottomMargin;
if (widthUsed + childUsedWidth < widthSpecSize) {
widthUsed += childUsedWidth;
if (childUsedHeight > childMaxHeightOfThisLine) {
childMaxHeightOfThisLine = childUsedHeight;
}
} else {
heightUsed += childMaxHeightOfThisLine + lineSpacing;
widthUsed = paddingLeft + paddingRight + childUsedWidth;
childMaxHeightOfThisLine = childUsedHeight;
}
}
}
heightUsed += childMaxHeightOfThisLine;
setMeasuredDimension(widthSpecSize, heightUsed);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
int paddingTop = getPaddingTop();
int childStartLayoutX = paddingLeft;
int childStartLayoutY = paddingTop;
int widthUsed = paddingLeft + paddingRight;
int childMaxHeight = 0;
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
if (child.getVisibility() != GONE) {
int childNeededWidth, childNeedHeight;
int left, top, right, bottom;
int childMeasuredWidth = child.getMeasuredWidth();
int childMeasuredHeight = child.getMeasuredHeight();
FlowLayoutParams params = (FlowLayoutParams) child
.getLayoutParams();
int childLeftMargin = params.leftMargin;
int childTopMargin = params.topMargin;
int childRightMargin = params.rightMargin;
int childBottomMargin = params.bottomMargin;
childNeededWidth = childLeftMargin + childRightMargin
+ childMeasuredWidth;
childNeedHeight = childTopMargin + childBottomMargin
+ childMeasuredHeight;
if (widthUsed + childNeededWidth <= r - l) {
if (childNeedHeight > childMaxHeight) {
childMaxHeight = childNeedHeight;
}
} else {
childStartLayoutY += childMaxHeight + lineSpacing;
childStartLayoutX = paddingLeft;
childMaxHeight = childNeedHeight;
widthUsed = paddingLeft + paddingRight;
}
left = childStartLayoutX + childLeftMargin;
top = childStartLayoutY + childTopMargin;
right = left + childMeasuredWidth;
bottom = top + childMeasuredHeight;
widthUsed += childNeededWidth;
childStartLayoutX += childNeededWidth;
child.layout(left, top, right, bottom);
}
}
}
@Override
protected LayoutParams generateLayoutParams(LayoutParams p) {
return new FlowLayoutParams(p);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new FlowLayoutParams(getContext(), attrs);
}
private class FlowLayoutParams extends MarginLayoutParams {
public FlowLayoutParams(int arg0, int arg1) {
super(arg0, arg1);
}
public FlowLayoutParams(LayoutParams arg0) {
super(arg0);
}
public FlowLayoutParams(MarginLayoutParams arg0) {
super(arg0);
}
public FlowLayoutParams(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
}
}
}
自定义ViewGroup实现流式布局

最新推荐文章于 2021-11-10 08:00:00 发布
