Skip to content

Commit 1494912

Browse files
committed
修复grid布局下的测量问题
1 parent adb9dde commit 1494912

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

.idea/vcs.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

library/src/main/java/com/example/library/AutoFlowLayout.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,13 @@ private void setGridMeasure(int widthMeasureSpec, int heightMeasureSpec) {
176176
int widthResult;
177177
//未设置行数 推测行数
178178
if (mRowNumbers == 0) {
179-
mRowNumbers = getChildCount()/mColumnNumbers == 0 ?
179+
mRowNumbers = getChildCount()%mColumnNumbers == 0 ?
180180
getChildCount()/mColumnNumbers : (getChildCount()/mColumnNumbers + 1);
181181
}
182182
int maxChildHeight = 0;
183183
int maxWidth = 0;
184184
int maxHeight = 0;
185+
int maxLineWidth = 0;
185186
//统计最大高度/最大宽度
186187
for (int i = 0; i < mRowNumbers; i++) {
187188
for (int j = 0; j < mColumnNumbers; j++) {
@@ -192,11 +193,13 @@ private void setGridMeasure(int widthMeasureSpec, int heightMeasureSpec) {
192193
// 得到child的lp
193194
MarginLayoutParams lp = (MarginLayoutParams) child
194195
.getLayoutParams();
195-
maxWidth +=child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;
196+
maxLineWidth +=child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;
196197
maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()+lp.topMargin+lp.bottomMargin);
197198
}
198199
}
199200
}
201+
maxWidth = Math.max(maxLineWidth,maxWidth);
202+
maxLineWidth = 0;
200203
maxHeight += maxChildHeight;
201204
maxChildHeight = 0;
202205
}

0 commit comments

Comments
 (0)