1010import androidx .annotation .Nullable ;
1111
1212import com .zhengsr .tablib .FlowConstants ;
13+ import com .zhengsr .tablib .bean .BaseLabelItem ;
14+ import com .zhengsr .tablib .view .adapter .BaseFlowAdapter ;
1315
1416import java .util .ArrayList ;
1517import java .util .List ;
1820 * @author by zhengshaorui on 2019/10/8
1921 * Describe: 瀑布流布局,这个类只用来测量子控件,不做其他操作
2022 */
21- class FlowLayout extends ViewGroup {
23+ class FlowLayout < T extends BaseFlowAdapter > extends ViewGroup {
2224 private static final String TAG = "FlowLayout" ;
2325 protected int mViewWidth ;
2426 protected int mViewHeight ;
@@ -35,6 +37,7 @@ class FlowLayout extends ViewGroup {
3537 private int mLabelLines = -1 ;
3638 private boolean isLabelMoreLine ;
3739 protected int mLineWidth ;
40+ protected T mAdapter ;
3841
3942 public FlowLayout (Context context ) {
4043 this (context , null );
@@ -162,7 +165,7 @@ private void measureTabHorizontal(int widthMeasureSpec, int heightMeasureSpec) {
162165 int widthPixels = getResources ().getDisplayMetrics ().widthPixels ;
163166 if (MeasureSpec .EXACTLY == widthMode ) {
164167 widthPixels = widthSize ;
165- }else {
168+ } else {
166169 if (mVisibleCount != -1 ) {
167170 MarginLayoutParams parentParams = (MarginLayoutParams ) getLayoutParams ();
168171 widthPixels -= (getPaddingStart () + getPaddingEnd () + parentParams .leftMargin + parentParams .rightMargin );
@@ -271,9 +274,11 @@ private void measureVertical(int widthMeasureSpec, int heightMeasureSpec) {
271274 /**
272275 * 确定是否换行
273276 */
277+ boolean isFull = lineWidth + cWidth > (widthSize - (getPaddingLeft () + getPaddingRight ()));
278+ boolean isHeader = isHeader (i );
274279
275- if (lineWidth + cWidth > (widthSize - (getPaddingLeft () + getPaddingRight ()))) {
276280
281+ if (isFull ) {
277282 //换行
278283 height += lineHeight ;
279284
@@ -297,10 +302,32 @@ private void measureVertical(int widthMeasureSpec, int heightMeasureSpec) {
297302
298303 } else {
299304 //未换行
305+ if (isHeader ) {
306+ //查看上一个
307+ if (i > 0 && getChildAt (i - 1 ) != null ) {
308+ //// 如果当前子视图是头部视图,并且上一个子视图不为空,则另起一行
309+ height += lineHeight ;
310+ mLineHeights .add (lineHeight );
311+ mAllViews .add (lineViews );
312+ lineViews = new ArrayList <>();
313+ }
314+ }
300315 lineWidth += cWidth ;
301316 lineHeight = Math .max (lineHeight , cHeight );
302317 mLineWidth = Math .max (mLineWidth , lineWidth );
303318 lineViews .add (child );
319+
320+ if (isHeader ) {
321+ //换行
322+ height += lineHeight ;
323+
324+
325+ mLineHeights .add (lineHeight );
326+ mAllViews .add (lineViews );
327+ lineViews = new ArrayList <>();
328+ }
329+
330+
304331 }
305332
306333 //加最后一行
@@ -408,6 +435,15 @@ public boolean isVerticalMove() {
408435 return isVertical () || isLabelFlow ();
409436 }
410437
438+ private boolean isHeader (int index ) {
439+ if (mAdapter != null && mAdapter .getDatas () != null ) {
440+ Object bean = mAdapter .getDatas ().get (index );
441+ if (bean instanceof BaseLabelItem ) {
442+ return ((BaseLabelItem ) bean ).isHeader ;
443+ }
444+ }
445+ return false ;
446+ }
411447
412448 public boolean isLabelMoreLine () {
413449 return isLabelMoreLine ;
0 commit comments