-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFlowLayout.java
More file actions
135 lines (118 loc) · 4.58 KB
/
FlowLayout.java
File metadata and controls
135 lines (118 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.itheima.demo2;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
/**
* Created by Administrator on 2016/8/18.
*/
public class FlowLayout extends ViewGroup {
private int horizontalSpacing = 30;
private int verticalSpacing = 20;
private String TAG;
public FlowLayout(Context context) {
super(context);
}
public FlowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
ArrayList<Line> lineList = new ArrayList<>();
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//获取控件宽度
int width = MeasureSpec.getSize(widthMeasureSpec);
int noPaddingWidth = width - getPaddingLeft() - getPaddingRight();
Line line = new Line();
for (int i = 0; i < getChildCount(); i++) {
View childView = getChildAt(i);
childView.measure(0, 0);
if (line.getViewList().size() == 0) {
line.addView(childView);
} else if (line.getLineWidth() + horizontalSpacing + childView.getMeasuredWidth() > noPaddingWidth) {
lineList.add(line);
line = new Line();
line.addView(childView);
} else {
line.addView(childView);
}
if (i==getChildCount()-1){
lineList.add(line);
}
}
int hight = getPaddingTop() + getPaddingTop();
for (int i = 0; i < lineList.size(); i++) {
hight += lineList.get(i).getHeight();
}
hight += (lineList.size() - 1) * verticalSpacing;
setMeasuredDimension(width, hight);
Log.d(TAG, "集合长度onMeasure"+lineList.size());
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int paddingLift = getPaddingLeft();
int paddingTop = getPaddingTop();
Log.d(TAG, "集合长度"+lineList.size());
for (int i = 0; i < lineList.size(); i++) {
Line line = lineList.get(i);
if (i > 0) {
paddingTop += lineList.get(i-1).getHeight() + verticalSpacing;
}
ArrayList<View> viewList = line.getViewList();
Log.d(TAG, "每行View的个数: "+viewList.size());
if(viewList.size()==0){
continue;
}
int remainSpacing = getLineRemainSpacing(line);
int perSpacing = remainSpacing/viewList.size();
for (int j = 0; j < viewList.size(); j++) {
View view = viewList.get(j);
int widthSpec = MeasureSpec.makeMeasureSpec(view.getMeasuredWidth()+perSpacing,MeasureSpec.EXACTLY);
view.measure(widthSpec,0);
if (j == 0) {
view.layout(paddingLift, paddingTop, paddingLift+view.getMeasuredWidth(), paddingTop+view.getMeasuredHeight());
}else{
// int measuredLeft = getChildAt(j- 1).getMeasuredWidth()+paddingLift;
View preView = viewList.get(j - 1);
view.layout(preView.getRight()+horizontalSpacing, preView.getTop(), preView.getRight()+horizontalSpacing+view.getMeasuredWidth(), paddingTop+view.getMeasuredHeight());
}
}
}
lineList.clear();
}
private int getLineRemainSpacing(Line line) {
return getMeasuredWidth()-getPaddingLeft()-getPaddingRight()-line.getLineWidth();
}
class Line {
private ArrayList<View> viewList;//用来存放当前行所有的子View
private int width;//表示所有子View的宽+水平间距
private int height;//行的高度
public Line() {
viewList = new ArrayList<>();
}
public void addView(View view) {
if (!viewList.contains(view)) {
viewList.add(view);
if (viewList.size() == 1) {
width = view.getMeasuredWidth();
} else {
width += horizontalSpacing + view.getMeasuredWidth();
}
height = Math.max(height, view.getMeasuredHeight());
}
}
public ArrayList<View> getViewList() {
return viewList;
}
public int getHeight() {
return height;
}
public int getLineWidth() {
return width;
}
}
}