Created
June 10, 2016 13:20
-
-
Save vadimZasovin/9d09e6971b702b2031aa925d1c518aad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.appcraft.teleport.view; | |
import android.content.Context; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import com.appcraft.teleport.util.Util; | |
/** | |
* Created by Admin on 20.05.2016. | |
*/ | |
public class AutoWrapLinearLayout extends ViewGroup { | |
private static final int CHIPS_MARGINS_DP = 4; | |
private final int mMargins; | |
public AutoWrapLinearLayout(Context context) { | |
this(context, null); | |
} | |
public AutoWrapLinearLayout(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public AutoWrapLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
mMargins = Util.Graphics.convertDpInPixels(context, CHIPS_MARGINS_DP); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
int width = MeasureSpec.getSize(widthMeasureSpec); | |
int height; | |
final int heightMode = MeasureSpec.getMode(heightMeasureSpec); | |
final int heightSize = MeasureSpec.getSize(heightMeasureSpec); | |
int childCount = getChildCount(); | |
if(heightMode != MeasureSpec.EXACTLY){ | |
int paddingLeft = getPaddingLeft(); | |
int paddingTop = getPaddingTop(); | |
int paddingRight = getPaddingRight(); | |
int paddingBottom = getPaddingBottom(); | |
int widthSpace = width - paddingLeft - paddingRight; | |
height = paddingTop + paddingBottom; | |
int consumedWidth = 0; | |
int maxChildHeight = -1; | |
int childHeight = 0; | |
for(int i = 0; i < childCount; i++){ | |
View child = getChildAt(i); | |
if(child.getVisibility() == GONE){ | |
continue; | |
} | |
measureChild(child, widthMeasureSpec, heightMeasureSpec); | |
int childWidth = child.getMeasuredWidth(); | |
childHeight = child.getMeasuredHeight(); | |
int availableWidth = widthSpace - consumedWidth; | |
if(childWidth > availableWidth){ | |
int rowHeight = maxChildHeight != -1 ? maxChildHeight : childHeight; | |
height += rowHeight + mMargins; | |
consumedWidth = 0; | |
maxChildHeight = -1; | |
} | |
if(childHeight > maxChildHeight){ | |
maxChildHeight = childHeight; | |
} | |
consumedWidth += childWidth + mMargins; | |
} | |
int rowHeight = maxChildHeight != -1 ? maxChildHeight : childHeight; | |
height += rowHeight + mMargins; | |
if(heightMode == MeasureSpec.AT_MOST && height > heightSize){ | |
height = heightSize; | |
} | |
}else { | |
for(int i = 0; i < childCount; i++){ | |
View child = getChildAt(i); | |
measureChild(child, widthMeasureSpec, heightMeasureSpec); | |
} | |
height = heightSize; | |
} | |
int suggestedHeight = getSuggestedMinimumHeight(); | |
if(height < suggestedHeight){ | |
height = suggestedHeight; | |
} | |
setMeasuredDimension(width, height); | |
} | |
@Override | |
protected void onLayout(boolean changed, int l, int t, int r, int b) { | |
final int paddingLeft = getPaddingLeft(); | |
final int paddingRight = getPaddingRight(); | |
int childLeft; | |
int childTop = getPaddingTop(); | |
int parentWidthSpace = r - l - paddingLeft - paddingRight; | |
final int childCount = getChildCount(); | |
int consumedWidth = 0; | |
int maxRowHeight = 0; | |
for(int i = 0; i < childCount; i++){ | |
View child = getChildAt(i); | |
if(child.getVisibility() == GONE){ | |
continue; | |
} | |
int childWidth = child.getMeasuredWidth(); | |
int childHeight = child.getMeasuredHeight(); | |
if(childWidth > parentWidthSpace - consumedWidth){ | |
childLeft = paddingLeft; | |
consumedWidth = 0; | |
childTop += maxRowHeight + mMargins; | |
maxRowHeight = 0; | |
}else { | |
childLeft = consumedWidth + paddingLeft; | |
} | |
int childRight = childLeft + childWidth; | |
int childBottom = childTop + childHeight; | |
child.layout(childLeft, childTop, childRight, childBottom); | |
consumedWidth += childWidth + mMargins; | |
if(childHeight > maxRowHeight){ | |
maxRowHeight = childHeight; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment