-
Notifications
You must be signed in to change notification settings - Fork 241
/
FlowLayout.java
228 lines (195 loc) · 8.37 KB
/
FlowLayout.java
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package com.d.lib.ui.view.flowlayout;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import com.d.lib.ui.view.R;
/**
* FlowLayout
* Created by D on 2018/7/11.
*/
public class FlowLayout extends ViewGroup {
private final static int ROW_COUNT = 4;
private final float mLoadFactor = 1.75f;
private int mWidth;
private int mHeight;
private float mPaddingLeft, mPaddingTop, mPaddingRight, mPaddingBottom;
private float mDividerWidth, mDividerHeight;
private FlowLayoutAdapter mAdapter;
private SparseArray<View> mItemViews;
public FlowLayout(Context context) {
super(context);
init();
}
public FlowLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initAttr(context, attrs);
init();
}
public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttr(context, attrs);
init();
}
private void initAttr(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.lib_ui_view_FlowLayout);
float padding = typedArray.getDimension(R.styleable.lib_ui_view_FlowLayout_lib_ui_view_flow_padding, 0);
mPaddingLeft = typedArray.getDimension(R.styleable.lib_ui_view_FlowLayout_lib_ui_view_flow_paddingLeft, padding);
mPaddingTop = typedArray.getDimension(R.styleable.lib_ui_view_FlowLayout_lib_ui_view_flow_paddingTop, padding);
mPaddingRight = typedArray.getDimension(R.styleable.lib_ui_view_FlowLayout_lib_ui_view_flow_paddingRight, padding);
mPaddingBottom = typedArray.getDimension(R.styleable.lib_ui_view_FlowLayout_lib_ui_view_flow_paddingBottom, padding);
mDividerWidth = typedArray.getDimension(R.styleable.lib_ui_view_FlowLayout_lib_ui_view_flow_dividerWidth, 0);
mDividerHeight = typedArray.getDimension(R.styleable.lib_ui_view_FlowLayout_lib_ui_view_flow_dividerHeight, 0);
typedArray.recycle();
}
private void init() {
this.mItemViews = new SparseArray<>();
}
private void reset() {
removeAllViews();
int recyclers = mItemViews.size();
int size = mAdapter != null ? mAdapter.getCount() : 0;
if (size <= 0) {
mItemViews.clear();
} else if (size + (int) (ROW_COUNT * mLoadFactor) < recyclers) {
for (int i = size + (int) (ROW_COUNT * mLoadFactor); i < recyclers; i++) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
mItemViews.removeAt(i);
} else {
mItemViews.delete(i);
}
}
}
for (int i = 0; i < size; i++) {
View child = mAdapter.getView(i, mItemViews.get(i), this);
mItemViews.put(i, child);
addChild(this, child);
}
requestLayout();
}
@Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
@Override
protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
return new LayoutParams(lp);
}
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
final boolean measureMatchParentChildren = MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY;
if (count <= 0 || measureMatchParentChildren) {
// Not support mode
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.EXACTLY));
return;
}
mWidth = MeasureSpec.getSize(widthMeasureSpec);
int left = (int) mPaddingLeft;
int top = (int) mPaddingTop;
int maxHeightRow = 0;
for (int i = 0; i < count; i++) {
// Measure size for each child view in the ViewGroup
final View child = getChildAt(i);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final int childWidthMeasureSpec;
if (lp.width == LayoutParams.MATCH_PARENT) {
final int width = Math.max(0, MeasureSpec.getSize(widthMeasureSpec));
childWidthMeasureSpec = MeasureSpec.makeMeasureSpec((int) (width - mPaddingLeft - mPaddingRight),
MeasureSpec.EXACTLY);
} else {
childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
(int) (mPaddingLeft + mPaddingRight), lp.width);
}
final int childHeightMeasureSpec;
if (lp.height == LayoutParams.MATCH_PARENT) {
final int height = Math.max(0, MeasureSpec.getSize(heightMeasureSpec));
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec((int) (height - mPaddingTop - mPaddingBottom),
MeasureSpec.EXACTLY);
} else {
childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
(int) (mPaddingTop + mPaddingBottom), lp.height);
}
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
final int width = child.getMeasuredWidth();
final int height = child.getMeasuredHeight();
if (!(left == (int) mPaddingLeft && width == mWidth - mPaddingLeft - mPaddingRight)
&& left + width + mDividerWidth > mWidth - mPaddingRight) {
left = (int) mPaddingLeft;
top += maxHeightRow + mDividerHeight;
maxHeightRow = 0;
}
left += width + mDividerWidth;
maxHeightRow = Math.max(maxHeightRow, height);
}
final int totalHeight = (int) (top + maxHeightRow + mPaddingBottom);
mHeight = MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY ? totalHeight
: MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(mWidth, mHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
if (count <= 0) {
return;
}
int left = (int) mPaddingLeft;
int top = (int) mPaddingTop;
int maxHeightRow = 0;
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
final int width = child.getMeasuredWidth();
final int height = child.getMeasuredHeight();
if (!(left == (int) mPaddingLeft && width == mWidth - mPaddingLeft - mPaddingRight)
&& left + width + mDividerWidth > mWidth - mPaddingRight) {
left = (int) mPaddingLeft;
top += maxHeightRow + mDividerHeight;
maxHeightRow = 0;
}
// Layout horizontally for each child view in the ViewGroup
child.layout(left, top, left + width, top + height);
left += width + mDividerWidth;
maxHeightRow = Math.max(maxHeightRow, height);
}
}
private void addChild(ViewGroup group, View child) {
if (group == null || child == null) {
return;
}
if (child.getParent() != null) {
((ViewGroup) child.getParent()).removeView(child);
}
group.addView(child);
}
public void setAdapter(FlowLayoutAdapter adapter) {
this.mAdapter = adapter;
this.mAdapter.registerDataSetObserver(new FlowLayoutAdapter.DataSetObserver() {
@Override
public void notifyChanged() {
reset();
}
});
reset();
}
public static class LayoutParams extends MarginLayoutParams {
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
}
public LayoutParams(int width, int height) {
super(width, height);
}
public LayoutParams(MarginLayoutParams source) {
super(source);
}
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
}
}
}