效果
微信图片_20170727104655.png
实现
/**
* 弹性流式布局
*
* @author gavin.xiong 2017/7/26
*/
public class FlexboxLayout extends ViewGroup {
public FlexboxLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width = 0, height = 0;
int lineWidth = 0, lineHeight = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
measureChild(child, widthMeasureSpec, heightMeasureSpec);
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int childWidth = lp.leftMargin + child.getMeasuredWidth() + lp.rightMargin;
int childHeight = lp.topMargin + child.getMeasuredHeight() + lp.bottomMargin;
if (lineWidth + childWidth > widthSize - getPaddingLeft() - getPaddingRight()) {
width = Math.max(width, lineWidth);
height += lineHeight;
lineWidth = childWidth;
lineHeight = childHeight;
} else {
lineWidth += childWidth;
lineHeight = Math.max(lineHeight, childHeight);
}
// 如果是最后一行
if (i == getChildCount() - 1) {
width = Math.max(width, lineWidth);
height += lineHeight;
}
}
setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : width + getPaddingLeft() + getPaddingRight(),
heightMode == MeasureSpec.EXACTLY ? heightSize : height + getPaddingTop() + getPaddingBottom());
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int left = getPaddingLeft();
int top = getPaddingTop();
int lineWidth = 0, lineHeight = 0;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int childWidth = lp.leftMargin + child.getMeasuredWidth() + lp.rightMargin;
int childHeight = lp.topMargin + child.getMeasuredHeight() + lp.bottomMargin;
if (lineWidth + childWidth > getWidth() - getPaddingLeft() - getPaddingRight()) {
left = getPaddingLeft();
top += lineHeight;
lineWidth = childWidth;
lineHeight = childHeight;
} else {
lineWidth += childWidth;
lineHeight = Math.max(lineHeight, childHeight);
}
int cl = left + lp.leftMargin;
int ct = top + lp.topMargin;
int cr = cl + child.getMeasuredWidth();
int cb = ct + child.getMeasuredHeight();
child.layout(cl, ct, cr, cb);
left += lp.leftMargin + child.getMeasuredWidth() + lp.rightMargin;
}
}
}
网友评论