55import android .graphics .Canvas ;
66import android .graphics .Paint ;
77import android .graphics .drawable .Drawable ;
8- import android .support .v4 .view .GestureDetectorCompat ;
98import android .util .AttributeSet ;
109import android .view .View ;
10+ import android .view .ViewGroup ;
1111import android .widget .LinearLayout ;
1212import java .util .ArrayList ;
13- import java .util .HashMap ;
1413import java .util .List ;
15- import java .util .Map ;
16- import java .util .Set ;
1714
1815/**
1916 *自定义LinearLayout,支持自动换行,指定行数,实现流式布局
2017 */
21- public class AutoFlowLayout <T > extends LinearLayout {
18+ public class AutoFlowLayout <T > extends ViewGroup {
2219 /**
2320 * 存储所有的View,按行记录
2421 */
@@ -150,11 +147,9 @@ private void init(Context context, AttributeSet attrs) {
150147 mIsGridMode = true ;
151148 }
152149 ta .recycle ();
153- setOrientation (HORIZONTAL );
154150 }
155151 @ Override
156152 protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
157- super .onMeasure (widthMeasureSpec , heightMeasureSpec );
158153 if (mIsGridMode ) {
159154 setGridMeasure (widthMeasureSpec ,heightMeasureSpec );
160155 } else {
@@ -197,12 +192,12 @@ private void setGridMeasure(int widthMeasureSpec, int heightMeasureSpec) {
197192 final View child = getChildAt (i * mColumnNumbers + j );
198193 if (child != null ) {
199194 if (child .getVisibility () != GONE ) {
200- final LayoutParams lp = ( LayoutParams ) child . getLayoutParams ( );
201- final int childWidthMeasureSpec = getChildMeasureSpec ( widthMeasureSpec , 0 , lp . width );
202- final int childHeightMeasureSpec = getChildMeasureSpec ( heightMeasureSpec , 0 , lp . height );
203- child . measure ( childWidthMeasureSpec , childHeightMeasureSpec );
204- maxWidth +=child .getMeasuredWidth ();
205- maxChildHeight = Math .max (maxChildHeight , child .getMeasuredHeight ());
195+ measureChild ( child , widthMeasureSpec , heightMeasureSpec );
196+ // 得到child的lp
197+ FlowLayoutParams lp = ( FlowLayoutParams ) child
198+ . getLayoutParams ( );
199+ maxWidth +=child .getMeasuredWidth ()+ lp . leftMargin + lp . rightMargin ;
200+ maxChildHeight = Math .max (maxChildHeight , child .getMeasuredHeight ()+ lp . topMargin + lp . bottomMargin );
206201 }
207202 }
208203 }
@@ -258,8 +253,8 @@ private void setFlowMeasure(int widthMeasureSpec, int heightMeasureSpec) {
258253 // 测量每一个child的宽和高
259254 measureChild (child , widthMeasureSpec , heightMeasureSpec );
260255 // 得到child的lp
261- MarginLayoutParams lp = (MarginLayoutParams ) child
262- .getLayoutParams ();
256+ FlowLayoutParams lp = (FlowLayoutParams ) child
257+ .getLayoutParams ();
263258 // 当前子空间实际占据的宽度
264259 int childWidth = child .getMeasuredWidth () + lp .leftMargin
265260 + lp .rightMargin ;
@@ -303,7 +298,6 @@ private void setFlowMeasure(int widthMeasureSpec, int heightMeasureSpec) {
303298 : height );
304299 }
305300
306-
307301 @ Override
308302 protected void onLayout (boolean changed , int l , int t , int r , int b ) {
309303 if (mIsGridMode ) {
@@ -320,18 +314,21 @@ private void setGridLayout() {
320314 mCurrentItemIndex = -1 ;
321315 int sizeWidth = getWidth ();
322316 int sizeHeight = getHeight ();
323- //子View的平均宽高
324- int childAvWidth = (int ) ((sizeWidth - getPaddingLeft () - getPaddingRight () - mHorizontalSpace * (mColumnNumbers -1 ))/mColumnNumbers );
325- int childAvHeight = (int ) ((sizeHeight - getPaddingTop () - getPaddingBottom () - mVerticalSpace * (mRowNumbers -1 ))/mRowNumbers );
317+ //子View的平均宽高 默认所有View宽高一致
318+ View tempChild = getChildAt (0 );
319+ FlowLayoutParams lp = (FlowLayoutParams ) tempChild
320+ .getLayoutParams ();
321+ int childAvWidth = (int ) ((sizeWidth - getPaddingLeft () - getPaddingRight () - mHorizontalSpace * (mColumnNumbers -1 ))/mColumnNumbers )-lp .leftMargin -lp .rightMargin ;
322+ int childAvHeight = (int ) ((sizeHeight - getPaddingTop () - getPaddingBottom () - mVerticalSpace * (mRowNumbers -1 ))/mRowNumbers )-lp .topMargin -lp .bottomMargin ;
326323 for (int i = 0 ; i < mRowNumbers ; i ++) {
327324 for (int j = 0 ; j < mColumnNumbers ; j ++) {
328325 final View child = getChildAt (i * mColumnNumbers + j );
329326 if (child != null ) {
330327 mCurrentItemIndex ++;
331328 if (child .getVisibility () != View .GONE ) {
332329 setChildClickOperation (child , -1 );
333- int childLeft = (int ) (getPaddingLeft () + j * (childAvWidth + mHorizontalSpace ));
334- int childTop = (int ) (getPaddingTop () + i * (childAvHeight + mVerticalSpace ));
330+ int childLeft = (int ) (getPaddingLeft () + j * (childAvWidth + mHorizontalSpace ))+ j * ( lp . leftMargin + lp . rightMargin ) + lp . leftMargin ;
331+ int childTop = (int ) (getPaddingTop () + i * (childAvHeight + mVerticalSpace )) + i * ( lp . topMargin + lp . bottomMargin ) + lp . topMargin ;
335332 child .layout (childLeft , childTop , childLeft + childAvWidth , childAvHeight +childTop );
336333 }
337334 }
@@ -359,7 +356,7 @@ private void setFlowLayout() {
359356 // 遍历所有的孩子
360357 for (int i = 0 ; i < cCount ; i ++) {
361358 View child = getChildAt (i );
362- MarginLayoutParams lp = (MarginLayoutParams ) child
359+ FlowLayoutParams lp = (FlowLayoutParams ) child
363360 .getLayoutParams ();
364361 int childWidth = child .getMeasuredWidth ();
365362 int childHeight = child .getMeasuredHeight ();
@@ -420,7 +417,7 @@ private void setFlowLayout() {
420417 continue ;
421418 }
422419 setChildClickOperation (child , -1 );
423- MarginLayoutParams lp = (MarginLayoutParams ) child
420+ FlowLayoutParams lp = (FlowLayoutParams ) child
424421 .getLayoutParams ();
425422
426423 //计算childView的left,top,right,bottom
@@ -435,9 +432,48 @@ private void setFlowLayout() {
435432 left += child .getMeasuredWidth () + lp .rightMargin
436433 + lp .leftMargin ;
437434 }
435+ FlowLayoutParams lp = (FlowLayoutParams ) getChildAt (0 )
436+ .getLayoutParams ();
438437 left = getPaddingLeft ();
439- top += lineHeight ;
438+ top += lineHeight + lp .topMargin + lp .bottomMargin ;
439+ }
440+ }
441+ public static class FlowLayoutParams extends MarginLayoutParams {
442+
443+ public FlowLayoutParams (Context c , AttributeSet attrs ) {
444+ super (c , attrs );
440445 }
446+
447+ public FlowLayoutParams (int width , int height ) {
448+ super (width , height );
449+ }
450+
451+ public FlowLayoutParams (ViewGroup .LayoutParams source ) {
452+ super (source );
453+ }
454+ }
455+
456+ @ Override
457+ public LayoutParams generateLayoutParams (AttributeSet attrs )
458+ {
459+ return new FlowLayoutParams (getContext (), attrs );
460+ }
461+
462+ @ Override
463+ protected LayoutParams generateDefaultLayoutParams ()
464+ {
465+ return new FlowLayoutParams (LayoutParams .WRAP_CONTENT , LayoutParams .WRAP_CONTENT );
466+ }
467+
468+ @ Override
469+ protected LayoutParams generateLayoutParams (LayoutParams p )
470+ {
471+ return new FlowLayoutParams (p );
472+ }
473+
474+ @ Override
475+ protected boolean checkLayoutParams (LayoutParams p ) {
476+ return p instanceof FlowLayoutParams ;
441477 }
442478
443479 /**
0 commit comments