老样子,先上效果图
标签容器这个在github上已经有很多人实现了,既然有轮子那为什么还要重复制造轮子呢? 本人比较懒,直接参考了https://github.com/XuYongJun96/FlowLayout 在它的基础上扩展了添加标签与删除标签的功能。
思路
既然是标签容器,那当然就用到了ViewGroup了。 重写onMeasure与onLayout方法。 在onMeasure计算每个标签的宽高, 在onLayout重新定位标签的位置。 从而做到标签能够自动换行显示。
核心代码
public class FlowLayout extends ViewGroup {
public FlowLayout(Context context) {
this(context,
null);
}
public FlowLayout(Context context, AttributeSet attrs) {
this(context, attrs,
0);
}
public FlowLayout(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(
int widthMeasureSpec,
int heightMeasureSpec) {
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
int width =
0;
int height =
0;
int lineWidth =
0;
int lineHeight =
0;
int childCount = getChildCount();
for (
int i =
0; i < childCount; i++) {
View child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
if (lineWidth + childWidth > sizeWidth - getPaddingLeft() - getPaddingRight()) {
width = Math.max(lineWidth, width);
lineWidth = childWidth;
height += lineHeight;
lineHeight = childHeight;
}
else {
lineWidth += childWidth;
lineHeight = Math.max(lineHeight, childHeight);
}
if (i == childCount -
1) {
width = Math.max(lineWidth, width);
height += lineHeight;
}
}
setMeasuredDimension(modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width + getPaddingRight() + getPaddingLeft(),
modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height + getPaddingBottom() + getPaddingTop());
}
/**
* 存放所有的子View
*/
private List<List<View>> mAllViews =
new ArrayList<List<View>>();
/**
* 每一行的高度
*/
private List<Integer> mLineHeight =
new ArrayList<Integer>();
@Override
protected void onLayout(
boolean changed,
int l,
int t,
int r,
int b) {
mAllViews.clear();
mLineHeight.clear();
int width = getWidth();
int lineWidth =
0;
int lineHeight =
0;
List<View> lineViews =
new ArrayList<View>();
int childCount = getChildCount();
for (
int i =
0; i < childCount; i++) {
View child = getChildAt(i);
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
if (lineWidth + childWidth + lp.leftMargin + lp.rightMargin > width - getPaddingLeft() -getPaddingRight()) {
mLineHeight.add(lineHeight);
System.out.println(
"lineVIews:" + lineViews.size());
mAllViews.add(lineViews);
lineWidth =
0;
lineHeight = childHeight + lp.bottomMargin + lp.topMargin;
lineViews =
new ArrayList<>();
}
lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
lineHeight = Math.max(lineHeight, childHeight + lp.bottomMargin + lp.topMargin);
lineViews.add(child);
}
mLineHeight.add(lineHeight);
mAllViews.add(lineViews);
int left =
0 + getPaddingLeft();
int top =
0 + getPaddingTop();
int lineNum = mAllViews.size();
for (
int i =
0; i < lineNum; i++) {
lineViews = mAllViews.get(i);
lineHeight = mLineHeight.get(i);
for (
int j =
0; j < lineViews.size(); j++) {
View child = lineViews.get(j);
if (child.getVisibility() == View.GONE) {
continue;
}
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int lc = left + lp.leftMargin;
int tc = top + lp.topMargin;
int rc = lc + child.getMeasuredWidth();
int bc = tc + child.getMeasuredHeight();
child.layout(lc, tc, rc, bc);
left += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
}
left =
0 + getPaddingLeft();
top += lineHeight;
}
}
/**
* 与当前ViewGroup对应的LayoutParams,也是子View的获取得到的
*
* @param attrs
* @return
*/
@Override
public LayoutParams
generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}
}
源码下载
转载请注明原文地址: https://ju.6miu.com/read-1297796.html