|
| 1 | +package com.lidong.android_ibrary.tagflowlayout; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.res.TypedArray; |
| 5 | +import android.util.AttributeSet; |
| 6 | +import android.view.View; |
| 7 | +import android.view.ViewGroup; |
| 8 | + |
| 9 | +import com.lidong.android_ibrary.R; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | +/** |
| 14 | +*@类名 : FlowLayout |
| 15 | +*@描述 : |
| 16 | +*@时间 : 2016/4/15 8:53 |
| 17 | +*@作者: 李东 |
| 18 | + |
| 19 | +*@company: chni |
| 20 | +*/ |
| 21 | +public class FlowLayout extends ViewGroup |
| 22 | +{ |
| 23 | + private static final String TAG = "FlowLayout"; |
| 24 | + private static final int LEFT = -1; |
| 25 | + private static final int CENTER = 0; |
| 26 | + private static final int RIGHT = 1; |
| 27 | + |
| 28 | + protected List<List<View>> mAllViews = new ArrayList<List<View>>(); |
| 29 | + protected List<Integer> mLineHeight = new ArrayList<Integer>(); |
| 30 | + protected List<Integer> mLineWidth = new ArrayList<Integer>(); |
| 31 | + private int mGravity; |
| 32 | + private List<View> lineViews = new ArrayList<>(); |
| 33 | + |
| 34 | + public FlowLayout(Context context, AttributeSet attrs, int defStyle) |
| 35 | + { |
| 36 | + super(context, attrs, defStyle); |
| 37 | + TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TagFlowLayout); |
| 38 | + mGravity = ta.getInt(R.styleable.TagFlowLayout_gravity,LEFT); |
| 39 | + ta.recycle(); |
| 40 | + } |
| 41 | + |
| 42 | + public FlowLayout(Context context, AttributeSet attrs) |
| 43 | + { |
| 44 | + this(context, attrs, 0); |
| 45 | + } |
| 46 | + |
| 47 | + public FlowLayout(Context context) |
| 48 | + { |
| 49 | + this(context, null); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) |
| 54 | + { |
| 55 | + int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); |
| 56 | + int modeWidth = MeasureSpec.getMode(widthMeasureSpec); |
| 57 | + int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); |
| 58 | + int modeHeight = MeasureSpec.getMode(heightMeasureSpec); |
| 59 | + |
| 60 | + // wrap_content |
| 61 | + int width = 0; |
| 62 | + int height = 0; |
| 63 | + |
| 64 | + int lineWidth = 0; |
| 65 | + int lineHeight = 0; |
| 66 | + |
| 67 | + int cCount = getChildCount(); |
| 68 | + |
| 69 | + for (int i = 0; i < cCount; i++) |
| 70 | + { |
| 71 | + View child = getChildAt(i); |
| 72 | + if (child.getVisibility() == View.GONE) |
| 73 | + { |
| 74 | + if (i == cCount - 1) |
| 75 | + { |
| 76 | + width = Math.max(lineWidth, width); |
| 77 | + height += lineHeight; |
| 78 | + } |
| 79 | + continue; |
| 80 | + } |
| 81 | + measureChild(child, widthMeasureSpec, heightMeasureSpec); |
| 82 | + MarginLayoutParams lp = (MarginLayoutParams) child |
| 83 | + .getLayoutParams(); |
| 84 | + |
| 85 | + int childWidth = child.getMeasuredWidth() + lp.leftMargin |
| 86 | + + lp.rightMargin; |
| 87 | + int childHeight = child.getMeasuredHeight() + lp.topMargin |
| 88 | + + lp.bottomMargin; |
| 89 | + |
| 90 | + if (lineWidth + childWidth > sizeWidth - getPaddingLeft() - getPaddingRight()) |
| 91 | + { |
| 92 | + width = Math.max(width, lineWidth); |
| 93 | + lineWidth = childWidth; |
| 94 | + height += lineHeight; |
| 95 | + lineHeight = childHeight; |
| 96 | + } else |
| 97 | + { |
| 98 | + lineWidth += childWidth; |
| 99 | + lineHeight = Math.max(lineHeight, childHeight); |
| 100 | + } |
| 101 | + if (i == cCount - 1) |
| 102 | + { |
| 103 | + width = Math.max(lineWidth, width); |
| 104 | + height += lineHeight; |
| 105 | + } |
| 106 | + } |
| 107 | + setMeasuredDimension( |
| 108 | + // |
| 109 | + modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width + getPaddingLeft() + getPaddingRight(), |
| 110 | + modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height + getPaddingTop() + getPaddingBottom()// |
| 111 | + ); |
| 112 | + |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + @Override |
| 117 | + protected void onLayout(boolean changed, int l, int t, int r, int b) |
| 118 | + { |
| 119 | + mAllViews.clear(); |
| 120 | + mLineHeight.clear(); |
| 121 | + mLineWidth.clear(); |
| 122 | + lineViews.clear(); |
| 123 | + |
| 124 | + int width = getWidth(); |
| 125 | + |
| 126 | + int lineWidth = 0; |
| 127 | + int lineHeight = 0; |
| 128 | + |
| 129 | + int cCount = getChildCount(); |
| 130 | + |
| 131 | + for (int i = 0; i < cCount; i++) |
| 132 | + { |
| 133 | + View child = getChildAt(i); |
| 134 | + if (child.getVisibility() == View.GONE) continue; |
| 135 | + MarginLayoutParams lp = (MarginLayoutParams) child |
| 136 | + .getLayoutParams(); |
| 137 | + |
| 138 | + int childWidth = child.getMeasuredWidth(); |
| 139 | + int childHeight = child.getMeasuredHeight(); |
| 140 | + |
| 141 | + if (childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width - getPaddingLeft() - getPaddingRight()) |
| 142 | + { |
| 143 | + mLineHeight.add(lineHeight); |
| 144 | + mAllViews.add(lineViews); |
| 145 | + mLineWidth.add(lineWidth); |
| 146 | + |
| 147 | + lineWidth = 0; |
| 148 | + lineHeight = childHeight + lp.topMargin + lp.bottomMargin; |
| 149 | + lineViews = new ArrayList<View>(); |
| 150 | + } |
| 151 | + lineWidth += childWidth + lp.leftMargin + lp.rightMargin; |
| 152 | + lineHeight = Math.max(lineHeight, childHeight + lp.topMargin |
| 153 | + + lp.bottomMargin); |
| 154 | + lineViews.add(child); |
| 155 | + |
| 156 | + } |
| 157 | + mLineHeight.add(lineHeight); |
| 158 | + mLineWidth.add(lineWidth); |
| 159 | + mAllViews.add(lineViews); |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | + int left = getPaddingLeft(); |
| 164 | + int top = getPaddingTop(); |
| 165 | + |
| 166 | + int lineNum = mAllViews.size(); |
| 167 | + |
| 168 | + for (int i = 0; i < lineNum; i++) |
| 169 | + { |
| 170 | + lineViews = mAllViews.get(i); |
| 171 | + lineHeight = mLineHeight.get(i); |
| 172 | + |
| 173 | + // set gravity |
| 174 | + int currentLineWidth = this.mLineWidth.get(i); |
| 175 | + switch (this.mGravity){ |
| 176 | + case LEFT: |
| 177 | + left = getPaddingLeft(); |
| 178 | + break; |
| 179 | + case CENTER: |
| 180 | + left = (width - currentLineWidth)/2+getPaddingLeft(); |
| 181 | + break; |
| 182 | + case RIGHT: |
| 183 | + left = width - currentLineWidth + getPaddingLeft(); |
| 184 | + break; |
| 185 | + } |
| 186 | + |
| 187 | + for (int j = 0; j < lineViews.size(); j++) |
| 188 | + { |
| 189 | + View child = lineViews.get(j); |
| 190 | + if (child.getVisibility() == View.GONE) |
| 191 | + { |
| 192 | + continue; |
| 193 | + } |
| 194 | + |
| 195 | + MarginLayoutParams lp = (MarginLayoutParams) child |
| 196 | + .getLayoutParams(); |
| 197 | + |
| 198 | + int lc = left + lp.leftMargin; |
| 199 | + int tc = top + lp.topMargin; |
| 200 | + int rc = lc + child.getMeasuredWidth(); |
| 201 | + int bc = tc + child.getMeasuredHeight(); |
| 202 | + |
| 203 | + child.layout(lc, tc, rc, bc); |
| 204 | + |
| 205 | + left += child.getMeasuredWidth() + lp.leftMargin |
| 206 | + + lp.rightMargin; |
| 207 | + } |
| 208 | + top += lineHeight; |
| 209 | + } |
| 210 | + |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public LayoutParams generateLayoutParams(AttributeSet attrs) |
| 215 | + { |
| 216 | + return new MarginLayoutParams(getContext(), attrs); |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + protected LayoutParams generateDefaultLayoutParams() |
| 221 | + { |
| 222 | + return new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 223 | + } |
| 224 | + |
| 225 | + @Override |
| 226 | + protected LayoutParams generateLayoutParams(LayoutParams p) |
| 227 | + { |
| 228 | + return new MarginLayoutParams(p); |
| 229 | + } |
| 230 | +} |
0 commit comments