学习自定义ViewGroup 实现tagView

看了上一篇,对view的绘制过程有了一定了解了。 下面动手做一个。

学习自定义ViewGroup 实现tagView

网上有很多flowlayout,可参考他们的实现。这里的目的是学习view的绘制过程。部分参考网上代码

//简单的tagview实现就是,多个tag,计算每个tag的长宽,计算当前行能显示几个,不够长了就换下一行显示。

顺序:measure()-onMeasure()-layout()-onLayout()-draw()-onDraw();
这个顺序面试有见过考这个的。
一般自定义都是重写onXxxx开头的,不带的不可以重写
例:viewGoup:

    @Override
    public final void layout(int l, int t, int r, int b) {}
    @Override
    protected abstract void onLayout(boolean changed,
            int l, int t, int r, int b);

重写onMeasure

重要地方都有注释~
需要for循环所有子类,计算子类的高度。-----此处默认高度都是一致的

 private int mTotalLength;
 private final int margin = 5;
 void measureVertical(int widthMeasureSpec, int heightMeasureSpec) {
        mTotalLength = 0;
        //此处计算的是当前view的大小
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        int count = getChildCount();
        //计算每个子类的大小
        int maxCalcWidth = 0;//最大宽度,当前view的width为wrap_content时有效
        int singleWidth = 0;//当前行宽度
        int realMaxWidth = sizeWidth - getPaddingLeft() - getPaddingRight();//去除当前view的padding就是子类可使用的大小
        mTotalLength += getPaddingTop() + getPaddingBottom();
        for (int i = 0; i < count; i++) {
            InnerTv child = (InnerTv) getChildAt(i);//InnerTv继承textView啥都没做,可以不用重写子view的onMeasure。。

            if (child == null) {
                continue;
            }
            measureChild(child, widthMeasureSpec, heightMeasureSpec);//计算子view的宽高

            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            if (i == 0) {
                //首高
                mTotalLength += childHeight;// 所有item高度一致,不换行
            }

            if (singleWidth + childWidth + margin > realMaxWidth) {//超出最大宽度
                //下一行
                maxCalcWidth = Math.max(maxCalcWidth, singleWidth);
                singleWidth = childWidth;
                mTotalLength += childHeight + margin;

            } else {
                if (i != 0) {
                    singleWidth += margin;
                }
                singleWidth += childWidth;
            }

            //最后一行
            if (i == count - 1) {
                maxCalcWidth = Math.max(maxCalcWidth, singleWidth);
            }
        }
        //设置最终高度
        setMeasuredDimension(
                modeWidth == MeasureSpec.EXACTLY ? sizeWidth : maxCalcWidth,
                modeHeight == MeasureSpec.EXACTLY ? sizeHeight : mTotalLength
        );

    }

onLayout()

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        int realWidth = r - l - getPaddingLeft() - getPaddingRight();
        int left = getPaddingLeft();
        int top = getPaddingTop();
        int linew = 1;
        for (int i = 0; i < count; i++) {
            View view = getChildAt(i);
            int vw = view.getMeasuredWidth();
            int vh = view.getMeasuredHeight();
            int vr = left + vw;
            if (vr > realWidth) {//超过屏幕了,换行
                linew++;
                left = getPaddingLeft();
                top += vh + margin;
                vr = vw;
            }
            view.layout(left, top, vr, top + vh);
            left += vw + margin;//
        }
    }

其他测试代码

    public void addAllTag() {
        for (int i = 0; i < contents.length; i++) {
            InnerTv tv = new InnerTv(getContext());
            LayoutParams lps = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            tv.setText(contents[i]);
            tv.setBackgroundColor(Color.GREEN);
            tv.setGravity(Gravity.CENTER);
            tv.setPadding(10, 5, 10, 5);
            addView(tv, lps);
        }
    }


    private String[] contents = new String[]{
            "我我我",
            "我我我们",
            "我我我哦",
            "急啊急啊就我",
            "我安安",
            "我烦烦烦1",
            "我烦烦烦2",
            "我烦烦烦3",
            "我烦烦烦4",
            "我烦烦烦55",
            "我烦烦烦11",
            "我烦烦烦22",
            "我烦烦烦33",
            "我烦烦烦44",
            "我烦烦烦655",
            "我烦烦烦666",
            "我烦烦烦677",
            "我烦烦烦88",
            "我烦烦烦99",
            "我烦烦烦00"
    };

效果图如下

image

简单的tag就实现了,需要圆角的话,只需要给textview添加一个背景即可。

其他扩展

以上效果图,每行都有剩余,能不能让最后一行填满呢。
实际就是让最后一个item,宽度变化

尝试把宽度变长,
修改InnerTv的onMeasure,super.onMeasure 后调用 setMeasuredDimension(reWidth,getMeasuredHeight());
不过效果有点偏差, 那个textview是变宽了,但是文字没居中。。。
TextView的onMeasure中应该已经计算好了文字位置所以这样修改文字没局中

最终实现如下

    //上一个重新绘制
    int pre = i - 1;
    if (pre >= 0) {
         InnerTv prev = (InnerTv) getChildAt(pre);
         prev.reMeasure(realMaxWidth - singleWidth + prev.getMeasuredWidth());
         //重新计算
         measureChild(prev, widthMeasureSpec, heightMeasureSpec);
    }
                
 
 
 InnerTv重写onMeasure
  @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = widthMeasureSpec;
        if (reWidth != 0) {
        //宽度固定是reWidth,所以模式也强制改成MeasureSpec.EXACTLY,得到新的width
            width = MeasureSpec.makeMeasureSpec(reWidth, MeasureSpec.EXACTLY);
        }
        super.onMeasure(width, heightMeasureSpec);
    }

    private int reWidth;

    public void reMeasure(int width) {
        this.reWidth = width;
    }

效果图如下:

image

==大功告成==~~~~

说明:这代码只是学习,不够完善,margin都是直接写死的,已经LayoutParams也是不带margin,完善的需要再优化。
完整代码如下:

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

public class VerticalLinearLayout extends ViewGroup {
    public VerticalLinearLayout(Context context) {
        super(context);
    }

    public VerticalLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VerticalLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureVertical(widthMeasureSpec, heightMeasureSpec);
    }

    private int mTotalLength;
    private final int margin = 5;

    void measureVertical(int widthMeasureSpec, int heightMeasureSpec) {
        mTotalLength = 0;
        //此处计算的是当前view的大小
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        int count = getChildCount();
        //计算每个子类的大小
        int maxCalcWidth = 0;//最大宽度,当前view的width为wrap_content时有效
        int singleWidth = 0;//当前行宽度
        int realMaxWidth = sizeWidth - getPaddingLeft() - getPaddingRight();//去除当前view的padding就是子类可使用的大小
        mTotalLength += getPaddingTop() + getPaddingBottom();
        for (int i = 0; i < count; i++) {
            InnerTv child = (InnerTv) getChildAt(i);//InnerTv继承textView啥都没做,可以不用重写子view的onMeasure。。

            if (child == null) {
                continue;
            }
            measureChild(child, widthMeasureSpec, heightMeasureSpec);//计算子view的宽高

            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();
            if (i == 0) {
                //首高
                mTotalLength += childHeight;// 所有item高度一致,不换行
            }

            if (singleWidth + childWidth + margin > realMaxWidth) {//超出最大宽度
                //上一个重新绘制
                int pre = i - 1;
                if (pre >= 0) {
                    InnerTv prev = (InnerTv) getChildAt(pre);
                    prev.reMeasure(realMaxWidth - singleWidth + prev.getMeasuredWidth());
                    measureChild(prev, widthMeasureSpec, heightMeasureSpec);
                }
                //下一行
                maxCalcWidth = Math.max(maxCalcWidth, singleWidth);
                singleWidth = childWidth;
                mTotalLength += childHeight + margin;

            } else {
                if (i != 0) {
                    singleWidth += margin;
                }
                singleWidth += childWidth;
            }

            //最后一行
            if (i == count - 1) {
                maxCalcWidth = Math.max(maxCalcWidth, singleWidth);
            }
        }
        //设置最终高度
        setMeasuredDimension(
                modeWidth == MeasureSpec.EXACTLY ? sizeWidth : maxCalcWidth,
                modeHeight == MeasureSpec.EXACTLY ? sizeHeight : mTotalLength
        );

    }

    /**
     * 尾部那个长度增加,满屏
     *
     * @param changed
     * @param l
     * @param t
     * @param r
     * @param b
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        int realWidth = r - l - getPaddingLeft() - getPaddingRight();
        int left = getPaddingLeft();
        int top = getPaddingTop();
        int linew = 1;
        for (int i = 0; i < count; i++) {
            View view = getChildAt(i);
            int vw = view.getMeasuredWidth();
            int vh = view.getMeasuredHeight();
            int vr = left + vw;
            if (vr > realWidth) {
                linew++;
                left = getPaddingLeft();
                top += vh + margin;
                vr = vw;


            }
            view.layout(left, top, vr, top + vh);
            left += vw + margin;//
        }
    }

    public void addAllTag() {
        for (int i = 0; i < contents.length; i++) {
            InnerTv tv = new InnerTv(getContext());
            LayoutParams lps = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            tv.setText(contents[i]);
            tv.setBackgroundColor(Color.GREEN);
            tv.setGravity(Gravity.CENTER);
            tv.setPadding(10, 5, 10, 5);
            addView(tv, lps);
        }
    }


    private String[] contents = new String[]{
            "我我我",
            "我我我们",
            "我我我哦",
            "急啊急啊就我",
            "我安安",
            "我烦烦烦1",
            "我烦烦烦2",
            "我烦烦烦3",
            "我烦烦烦4",
            "我烦烦烦55",
            "我烦烦烦11",
            "我烦烦烦22",
            "我烦烦烦33",
            "我烦烦烦44",
            "我烦烦烦655",
            "我烦烦烦666",
            "我烦烦烦677",
            "我烦烦烦88",
            "我烦烦烦99",
            "我烦烦烦00"
    };


}

InnerTv

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;

public class InnerTv extends TextView {
    public InnerTv(Context context) {
        super(context);
    }

    public InnerTv(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public InnerTv(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = widthMeasureSpec;
        if (reWidth != 0) {
            width = MeasureSpec.makeMeasureSpec(reWidth, MeasureSpec.EXACTLY);
        }
        super.onMeasure(width, heightMeasureSpec);
    }

    private int reWidth;

    public void reMeasure(int width) {
        this.reWidth = width;
    }
}

其他

VerticalLinearLayout mf = findViewById(R.id.myflowlayout);
mf.addAllTag();

上面所说到的完善一些, 还会用到另外一个方法
generateDefaultLayoutParams()
返回当前view(父容器) 添加子View时,给子view的默认LayoutParams,以上代码的获取到的getLayoutParams得到的是ViewGroup.LayoutParams,没有margin,所以需要改成MarginLayoutParams

@Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }

该方法的具体实现可以看看FrameLayout、LinearLayout等等常用viewgroup.

//addview没有添加LayoutParams 就会默认生成一个
 public void addView(View child, int index) {
        if (child == null) {
            throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
        }
        LayoutParams params = child.getLayoutParams();
        if (params == null) {
            params = generateDefaultLayoutParams();
            if (params == null) {
                throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
            }
        }
        addView(child, index, params);
    }

以上就是大概的一个实现过程,动手学习~

最后~~

附上完整版
TagFlowLayout
https://github.com/zeng3234/TagFlowLayout

当然github有很多比较成熟的,比如鸿洋的.. 看哪个顺手用哪个~~

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,175评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,674评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,151评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,597评论 1 269
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,505评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,969评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,455评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,118评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,227评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,213评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,214评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,928评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,512评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,616评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,848评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,228评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,772评论 2 339

推荐阅读更多精彩内容