Skip to content

Commit 6c4799d

Browse files
committed
设置margin间距
1 parent 5047650 commit 6c4799d

3 files changed

Lines changed: 30 additions & 60 deletions

File tree

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

Lines changed: 23 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ private void setGridMeasure(int widthMeasureSpec, int heightMeasureSpec) {
190190
if (child.getVisibility() != GONE) {
191191
measureChild(child,widthMeasureSpec,heightMeasureSpec);
192192
// 得到child的lp
193-
FlowLayoutParams lp = (FlowLayoutParams) child
193+
MarginLayoutParams lp = (MarginLayoutParams) child
194194
.getLayoutParams();
195195
maxWidth +=child.getMeasuredWidth()+lp.leftMargin+lp.rightMargin;
196196
maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()+lp.topMargin+lp.bottomMargin);
@@ -249,7 +249,7 @@ private void setFlowMeasure(int widthMeasureSpec, int heightMeasureSpec) {
249249
// 测量每一个child的宽和高
250250
measureChild(child, widthMeasureSpec, heightMeasureSpec);
251251
// 得到child的lp
252-
FlowLayoutParams lp = (FlowLayoutParams) child
252+
MarginLayoutParams lp = (MarginLayoutParams) child
253253
.getLayoutParams();
254254
// 当前子空间实际占据的宽度
255255
int childWidth = child.getMeasuredWidth() + lp.leftMargin
@@ -312,7 +312,7 @@ private void setGridLayout() {
312312
int sizeHeight = getHeight();
313313
//子View的平均宽高 默认所有View宽高一致
314314
View tempChild = getChildAt(0);
315-
FlowLayoutParams lp = (FlowLayoutParams) tempChild
315+
MarginLayoutParams lp = (MarginLayoutParams) tempChild
316316
.getLayoutParams();
317317
int childAvWidth = (int) ((sizeWidth - getPaddingLeft() - getPaddingRight() - mHorizontalSpace * (mColumnNumbers-1))/mColumnNumbers)-lp.leftMargin-lp.rightMargin;
318318
int childAvHeight = (int) ((sizeHeight - getPaddingTop() - getPaddingBottom() - mVerticalSpace * (mRowNumbers-1))/mRowNumbers)-lp.topMargin-lp.bottomMargin;
@@ -352,7 +352,7 @@ private void setFlowLayout() {
352352
// 遍历所有的孩子
353353
for (int i = 0; i < cCount; i++) {
354354
View child = getChildAt(i);
355-
FlowLayoutParams lp = (FlowLayoutParams) child
355+
MarginLayoutParams lp = (MarginLayoutParams) child
356356
.getLayoutParams();
357357
int childWidth = child.getMeasuredWidth();
358358
int childHeight = child.getMeasuredHeight();
@@ -413,7 +413,7 @@ private void setFlowLayout() {
413413
continue;
414414
}
415415
setChildClickOperation(child, -1);
416-
FlowLayoutParams lp = (FlowLayoutParams) child
416+
MarginLayoutParams lp = (MarginLayoutParams) child
417417
.getLayoutParams();
418418

419419
//计算childView的left,top,right,bottom
@@ -428,50 +428,28 @@ private void setFlowLayout() {
428428
left += child.getMeasuredWidth() + lp.rightMargin
429429
+ lp.leftMargin;
430430
}
431-
FlowLayoutParams lp = (FlowLayoutParams) getChildAt(0)
431+
MarginLayoutParams lp = (MarginLayoutParams) getChildAt(0)
432432
.getLayoutParams();
433433
left = getPaddingLeft();
434434
top += lineHeight + lp.topMargin + lp.bottomMargin;
435435
}
436436
}
437-
public static class FlowLayoutParams extends MarginLayoutParams {
438437

439-
public FlowLayoutParams(Context c, AttributeSet attrs) {
440-
super(c, attrs);
441-
}
442-
443-
public FlowLayoutParams(int width, int height) {
444-
super(width, height);
445-
}
446-
447-
public FlowLayoutParams(ViewGroup.LayoutParams source) {
448-
super(source);
449-
}
450-
}
451438

452439
@Override
453440
public LayoutParams generateLayoutParams(AttributeSet attrs)
454441
{
455-
return new FlowLayoutParams(getContext(), attrs);
442+
return new MarginLayoutParams(getContext(), attrs);
456443
}
457444

458445
@Override
459-
protected LayoutParams generateDefaultLayoutParams()
460-
{
461-
return new FlowLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
446+
protected LayoutParams generateLayoutParams(LayoutParams p) {
447+
return new MarginLayoutParams(p);
462448
}
463-
464-
@Override
465-
protected LayoutParams generateLayoutParams(LayoutParams p)
466-
{
467-
return new FlowLayoutParams(p);
468-
}
469-
470449
@Override
471-
protected boolean checkLayoutParams(LayoutParams p) {
472-
return p instanceof FlowLayoutParams;
450+
protected LayoutParams generateDefaultLayoutParams() {
451+
return new MarginLayoutParams(super.generateDefaultLayoutParams());
473452
}
474-
475453
/**
476454
* 执行子View的点击相关事件
477455
* @param child
@@ -537,41 +515,29 @@ protected void dispatchDraw(Canvas canvas) {
537515
if (j == mColumnNumbers-1) {
538516
//不是最后一行 只画底部
539517
if (i != mRowNumbers-1){
540-
View nextChild = getChildAt(i * mColumnNumbers + j+mColumnNumbers);
541-
View preChild = getChildAt(i * mColumnNumbers + j - 1);
542-
canvas.drawLine((child.getLeft()+preChild.getRight())/2,(child.getBottom()+nextChild.getTop())/2,
543-
child.getRight(),(child.getBottom()+nextChild.getTop())/2,linePaint);
518+
canvas.drawLine(child.getLeft()-mHorizontalSpace/2,child.getBottom()+mVerticalSpace/2,
519+
child.getRight(),child.getBottom()+mVerticalSpace/2,linePaint);
544520
}
545521
} else {
546522
//最后一行 只画右部
547523
if (i == mRowNumbers -1) {
548-
View nextChild = getChildAt(i * mColumnNumbers + j + 1);
549-
View preChild = getChildAt(i * mColumnNumbers + j - mColumnNumbers);
550-
canvas.drawLine((child.getRight()+nextChild.getLeft())/2, (child.getTop()+preChild.getBottom())/2,
551-
(child.getRight()+nextChild.getLeft())/2,child.getBottom(),linePaint);
524+
canvas.drawLine(child.getRight()+mHorizontalSpace/2, child.getTop()-mVerticalSpace/2,
525+
child.getRight()+mHorizontalSpace/2,child.getBottom(),linePaint);
552526
} else {
553527
//底部 右部 都画
554-
View rightChild = getChildAt(i * mColumnNumbers + j + 1);
555-
View bottomChild = getChildAt(i * mColumnNumbers + j + mColumnNumbers);
556528
if (j == 0) {
557-
View followChild = getChildAt(i * mColumnNumbers + j + 1);
558-
canvas.drawLine(child.getLeft(),(child.getBottom()+bottomChild.getTop())/2,
559-
(child.getRight()+followChild.getLeft())/2,(child.getBottom()+bottomChild.getTop())/2,linePaint);
529+
canvas.drawLine(child.getLeft(),child.getBottom()+mVerticalSpace/2,
530+
child.getRight()+mHorizontalSpace/2,child.getBottom()+mVerticalSpace/2,linePaint);
560531
} else {
561-
View preChild = getChildAt(i * mColumnNumbers + j - 1);
562-
View followChild = getChildAt(i * mColumnNumbers + j + 1);
563-
canvas.drawLine((child.getLeft()+preChild.getRight())/2,(child.getBottom()+bottomChild.getTop())/2,
564-
(child.getRight()+followChild.getLeft())/2,(child.getBottom()+bottomChild.getTop())/2,linePaint);
532+
canvas.drawLine(child.getLeft()-mHorizontalSpace/2,child.getBottom()+mVerticalSpace/2,
533+
child.getRight()+mHorizontalSpace/2,child.getBottom()+mVerticalSpace/2,linePaint);
565534
}
566535
if (i == 0) {
567-
View followChild = getChildAt(i * mColumnNumbers + j + mColumnNumbers);
568-
canvas.drawLine((child.getRight()+rightChild.getLeft())/2, child.getTop(),
569-
(child.getRight()+rightChild.getLeft())/2,(child.getBottom()+followChild.getTop())/2,linePaint);
536+
canvas.drawLine(child.getRight()+mHorizontalSpace/2, child.getTop(),
537+
child.getRight()+mHorizontalSpace/2,child.getBottom()+mVerticalSpace/2,linePaint);
570538
} else {
571-
View preChild = getChildAt(i * mColumnNumbers + j - mColumnNumbers);
572-
View followChild = getChildAt(i * mColumnNumbers + j + mColumnNumbers);
573-
canvas.drawLine((child.getRight()+rightChild.getLeft())/2, (child.getTop()+preChild.getBottom())/2,
574-
(child.getRight()+rightChild.getLeft())/2,(child.getBottom()+followChild.getTop())/2,linePaint);
539+
canvas.drawLine(child.getRight()+mHorizontalSpace/2, child.getTop()-mVerticalSpace/2,
540+
child.getRight()+mHorizontalSpace/2,child.getBottom()+mVerticalSpace/2,linePaint);
575541
}
576542

577543
}
170 KB
Loading
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:orientation="vertical"
4-
android:layout_width="match_parent"
5-
android:layout_height="match_parent">
6-
4+
android:layout_width="100dp"
5+
android:layout_height="100dp">
6+
<ImageView
7+
android:src="@drawable/grid_icon"
8+
android:scaleType="centerCrop"
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent"/>
711
</LinearLayout>

0 commit comments

Comments
 (0)