android,自定义ViewGroup,tagView,横竖摆放view不重叠

项目需求,须tagview,网上有第三方view:tagview、TagFlowLayout

由于项目比较正式,需要尽量避免第三方库,找到一个tagview原理级别的viewgroupWordWrapView

集成到项目后,发现有两个bug:
1.同一行,如果高度不一样,那么会出现错误
2.child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);子view测量的时候没有边界,导致一些太长的文字显示出来的时候超出了边界。

修复后的代码:

public class TagView extends ViewGroup {
    private LayoutInflater mInflater;

    private static final int PADDING_HOR = 2;//水平方向padding
    private static final int PADDING_VERTICAL = 1;//垂直方向padding
    private static final int SIDE_MARGIN = 2;//左右间距
    private static final int TEXT_MARGIN = 2;//??

    public MyTagView(Context context) {
        super(context);
        this.initialize(context);
    }

    public MyTagView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initialize(context);
    }

    private void initialize(Context ctx) {
        this.mInflater = (LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
    }

    public void drawTags(final List<MTag> tags) {
        this.removeAllViews();

        for (MTag tag : tags) {
            String text = tag.getText();
            if (text == null) {
                text = "";
            }
            View view = mInflater.inflate(R.layout.item_tagaddnearby, null);
            AppTextView textView = (AppTextView) view.findViewById(R.id.tag_txt);
            textView.setText(text);

            this.addView(view);
        }
        this.requestLayout();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int autualWidth = r - l;
        int x = 0;// 横坐标开始
        int y = 0;//纵坐标开始
        int maxHeightInLine = 0;
        for (int i = 0; i < childCount; i++) {
            View view = getChildAt(i);
            int width = view.getMeasuredWidth();
            int height = view.getMeasuredHeight();

            x += width + TEXT_MARGIN;
            if (x > autualWidth) {
                x = width;
                y += (maxHeightInLine + TEXT_MARGIN);
                maxHeightInLine = height;
            } else {
                if (maxHeightInLine <= height) {
                    maxHeightInLine = height;
                }
            }

            if (i == 0) {
                view.layout(x - width - TEXT_MARGIN, y, x - TEXT_MARGIN, y + height);
            } else {
                view.layout(x - width, y, x, y + height);
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int x = 0;//横坐标
        int y = 0;//纵坐标
        int specWidth = MeasureSpec.getSize(widthMeasureSpec);
        int actualWidth = specWidth - SIDE_MARGIN * 2;//实际宽度
        int childCount = getChildCount();
        int maxHeightInLine = 0;
        for (int index = 0; index < childCount; index++) {
            View child = getChildAt(index);
            child.setPadding(PADDING_HOR, PADDING_VERTICAL, PADDING_HOR, PADDING_VERTICAL);
//            int[] max = DensityUtil.getWindowsSize(this, 0, 0);
//            int maxWidth = MeasureSpec.makeMeasureSpec(max[0]/2,MeasureSpec.AT_MOST);//指定最大宽度
            int maxWidth = MeasureSpec.makeMeasureSpec(actualWidth, MeasureSpec.AT_MOST);

            child.measure(maxWidth, MeasureSpec.UNSPECIFIED);
            int width = child.getMeasuredWidth();
            int height = child.getMeasuredHeight();

            x += width + TEXT_MARGIN;
            if (x > actualWidth) {//换行
                x = width;
                y += (maxHeightInLine + TEXT_MARGIN);
                maxHeightInLine = height;
            } else {
                if (maxHeightInLine <= height) {
                    maxHeightInLine = height;
                }
            }
        }
        y += maxHeightInLine + TEXT_MARGIN;

        setMeasuredDimension(actualWidth, y);
    }
}

最终效果如下:
这里写图片描述

参考:
http://www.tuicool.com/articles/u2eEbe
http://blog.csdn.net/lilybaobei/article/details/8021868
http://www.2cto.com/kf/201503/382697.html
http://825288003.iteye.com/blog/1770602

请访问我的博客浏览更多文章:http://blog.csdn.net/u010499721

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值