Skip to content

Commit 9dc2213

Browse files
committed
增加行居中显示
1 parent c4db7b5 commit 9dc2213

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

app/src/main/java/com/example/lvruheng/autoflowlayout/AutoFlowLayout.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
}

app/src/main/java/com/example/lvruheng/autoflowlayout/MainActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ protected void onCreate(Bundle savedInstanceState) {
2222
@Override
2323
public View getView(int position) {
2424
View item = mLayoutInflater.inflate(R.layout.sub_item, null);
25-
// TextView tvAttrTag = (TextView) item.findViewById(R.id.tv_attr_tag);
26-
// tvAttrTag.setText(mData[position]);
25+
TextView tvAttrTag = (TextView) item.findViewById(R.id.tv_attr_tag);
26+
tvAttrTag.setText(mData[position]);
2727
return item;
2828
}
2929
});

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77

88
<com.example.lvruheng.autoflowlayout.AutoFlowLayout
99
android:id="@+id/afl_cotent"
10-
app:columnNumbers="3"
11-
app:rowNumbers="3"
12-
app:cutLine="true"
10+
app:lineCenter = "true"
1311
android:layout_width="match_parent"
1412
android:layout_height="match_parent"/>
1513
</RelativeLayout>

0 commit comments

Comments
 (0)