@@ -19,6 +19,10 @@ public class AutoFlowLayout <T> extends LinearLayout {
1919 * 存储所有的View,按行记录
2020 */
2121 private List <List <View >> mAllViews = new ArrayList <List <View >>();
22+ /**
23+ * 记录每一行的宽度
24+ */
25+ private List <Integer > mWidthList = new ArrayList <>();
2226 /**
2327 * 记录设置单行显示的标志
2428 */
@@ -104,6 +108,10 @@ public class AutoFlowLayout <T> extends LinearLayout {
104108 *记录分割线的长度
105109 */
106110 private int mCutLineColor ;
111+ /**
112+ * 是否每行居中处理
113+ */
114+ private boolean mIsCenter ;
107115 public AutoFlowLayout (Context context ) {
108116 super (context );
109117 }
@@ -129,6 +137,7 @@ private void init(Context context, AttributeSet attrs) {
129137 mCutLineColor = ta .getColor (R .styleable .AutoFlowLayout_cutLineColor ,getResources ().getColor (android .R .color .darker_gray ));
130138 mCutLineWidth = ta .getDimension (R .styleable .AutoFlowLayout_cutLineWidth ,1f );
131139 mIsCutLine = ta .getBoolean (R .styleable .AutoFlowLayout_cutLine ,false );
140+ mIsCenter = ta .getBoolean (R .styleable .AutoFlowLayout_lineCenter ,false );
132141 if (mColumnNumbers != 0 ) {
133142 mIsGridMode = true ;
134143 }
@@ -352,6 +361,7 @@ private void setFlowLayout() {
352361 mLineHeight .add (lineHeight );
353362 // 将当前行的childView保存,然后开启新的ArrayList保存下一行的childView
354363 mAllViews .add (lineViews );
364+ mWidthList .add (lp .leftMargin + lp .rightMargin + getPaddingRight () + lineWidth );
355365 lineWidth = 0 ;// 重置行宽
356366 lineViews = new ArrayList <View >();
357367 mCount ++;
@@ -375,7 +385,7 @@ private void setFlowLayout() {
375385 // 记录最后一行
376386 mLineHeight .add (lineHeight );
377387 mAllViews .add (lineViews );
378-
388+ mWidthList . add ( lineWidth );
379389 int left = getPaddingLeft ();
380390 int top = getPaddingTop ();
381391 // 得到总行数
@@ -388,6 +398,11 @@ private void setFlowLayout() {
388398 lineViews = mAllViews .get (i );
389399 // 当前行的最大高度
390400 lineHeight = mLineHeight .get (i );
401+ if (mIsCenter ) {
402+ if (mWidthList .get (i )<getWidth ()) {
403+ left +=(getWidth ()-mWidthList .get (i ))/2 ;
404+ }
405+ }
391406 // 遍历当前行所有的View
392407 for (int j = 0 ; j < lineViews .size (); j ++) {
393408 final View child = lineViews .get (j );
@@ -805,6 +820,22 @@ public boolean hasCutLine() {
805820 return mIsCutLine ;
806821 }
807822
823+ /**
824+ * 设置是否进行行居中显示
825+ * @param isCenter
826+ */
827+ public void setLineCenter (boolean isCenter ) {
828+ mIsCenter = isCenter ;
829+ }
830+
831+ /**
832+ * 是否设置了行居中显示
833+ * @return
834+ */
835+ public boolean isLineCenter () {
836+ return mIsCenter ;
837+ }
838+
808839 public interface OnItemClickListener {
809840 void onItemClick (int position ,View view );
810841 }
0 commit comments