仿淘宝搜索历史界面(流式布局)

一 关于自定义viewGroup

自定义viewGroup需要继承viewGroup,并重写抽象方法onLayout,负责子view位置的摆放。onMeasure()方法则完成对子view的测量过程,其中,对子view的测量涉及到MeasureSpec,包括父布局的MeasureSpec和子view的MeasureSpec。对子view的宽高测量完成后,相加即为viewGroup的宽高,再通过setMeasuredDimension()这个方法来保存测量好的宽高。

二 关于MeasureSpec

MeasureSpec代表一个32位int值,高2位代表SpecMode,低30位代码SpecSize,SpecMode是指测量模式,而SpecSize是指在某种测量模式下地规格地大小。

SpecMode有三类,每一类都表示特殊的含义,如下所示。

UNSPECIFIED

父容器不对View有任何限制,要多大给多大,这种情况一般用于系统内部,表示一种测量状态。

EXACTLY

父容器以及检测出View所需要的大小,这个时候View的最终大小就是SpecSize所指定的值。它对应于LayoutParams中的match_parent和具体数值这两种模式。

AT_MOST

父容器指定了一个可用大小即SpecSize,View的大小不能大于这个值,具体是什么要看不同View的具体实现。它对应于LayoutParams中的wrap_content。

三 关于流式布局Measure流程

1.遍历所有的子view,根据父布局的MeasureSpec还有子view的LayoutParams 算出子view的MesureSpec


LayoutParams childLP = childView.getLayoutParams();

int childWidthMesureSpec =getChildMeasureSpec(widthMeasureSpec,paddingLeft+paddingRight,childLP.width);

int childHeightMesureSpec =getChildMeasureSpec(heightMeasureSpec,paddingBottom+paddingTop,childLP.height);

childView.measure(childWidthMesureSpec,childHeightMesureSpec);

2.通过宽度来判断是否需要换行,通过换行后的每行的行高来获取整个viewGroup的行高

如果宽度不够,则需换行


if (childMesuredWidth + lineWidthUsed +mHorizontalSpacing > selfWidth){

allLines.add(lineViews);

    lineHeights.add(lineHeight);

    //一旦换行,可以判断当前所需的宽高了

    parentNeededWidth = Math.max(parentNeededWidth,lineWidthUsed+mHorizontalSpacing);

    parentNeededHeight = parentNeededHeight + lineHeight +mVerticalSpacing;

    lineViews =new ArrayList<>();

    lineWidthUsed =0;

    lineHeight =0;

}

3.根据子View的度量结果,来重新度量自己ViewGroup

作为一个ViewGroup,它自己也是一个View,它的大小也需要根据它的父亲给它提供的宽高来度量


int widthMode = MeasureSpec.getMode(widthMeasureSpec);

int heightMode = MeasureSpec.getMode(heightMeasureSpec);

int realWidth = (widthMode == MeasureSpec.EXACTLY)? selfWidth:parentNeededWidth;

int realHeight = (heightMode == MeasureSpec.EXACTLY)?selfHeight:parentNeededHeight;

setMeasuredDimension(realWidth,realHeight);

四 关于layout流程

measure完成后,每个子view的宽高通过view.getMeasuredHeight/Width 来获得,只需算出距离viewGroup左边和顶部的距离传入即可。


int lineCount =allLines.size();

int curL =0;

int curT =0;

for (int i =0;i

List lineViews =allLines.get(i);

    int lineHeight =lineHeights.get(i);

    for (int j =0;j

View view = lineViews.get(j);

        int left = curL;

        int top = curT;

        int bottom = top + view.getMeasuredHeight();

        int right = left + view.getMeasuredWidth();

        view.layout(left,top,right,bottom);

        curL = right +mHorizontalSpacing;

    }

curL =0;

    curT = curT + lineHeight +mVerticalSpacing;

五 关于源代码FlowLayout源代码


/**

* created by lxx

* on 2020/4/22

*/

public class FlowLayoutextends ViewGroup {

private int mHorizontalSpacing =dp2px(16); //每个item横向间距

    private int mVerticalSpacing =dp2px(8); //每个item横向间距

    private  List>allLines ; // 记录所有的行,一行一行的存储

    private  ListlineHeights =new ArrayList<>(); // 记录每一行的行高

    boolean isMeasuredOver =false;

    public FlowLayout(Context context) {

super(context);

    }

public FlowLayout(Context context, AttributeSet attrs) {

super(context, attrs);

    }

public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

    }

@Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

initMeasureParams();

        int childCount = getChildCount();

        int paddingLeft = getPaddingLeft();

        int paddingRight = getPaddingRight();

        int paddingTop = getPaddingTop();

        int paddingBottom = getPaddingBottom();

        int selfWidth = MeasureSpec.getSize(widthMeasureSpec);  //ViewGroup解析的宽度

        int selfHeight = MeasureSpec.getSize(heightMeasureSpec); // ViewGroup解析的高度

        List lineViews =new ArrayList<>(); //保存一行中的所有的view

        int lineWidthUsed =0; //记录这行已经使用了多宽的size

        int lineHeight =0; // 一行的行高

        int parentNeededWidth =0;  // measure过程中,子View要求的父ViewGroup的宽

        int parentNeededHeight =0; // measure过程中,子View要求的父ViewGroup的高

        for (int i =0;i

View childView = getChildAt(i);

            LayoutParams childLP = childView.getLayoutParams();

            int childWidthMesureSpec =getChildMeasureSpec(widthMeasureSpec,paddingLeft+paddingRight,childLP.width);

            int childHeightMesureSpec =getChildMeasureSpec(heightMeasureSpec,paddingBottom+paddingTop,childLP.height);

            childView.measure(childWidthMesureSpec,childHeightMesureSpec);

            //获取子view的宽高

            int childMesuredWidth = childView.getMeasuredWidth();

            int childMesuredHeight = childView.getMeasuredHeight();

            //通过宽度来判断是否需要换行,通过换行后的每行的行高来获取整个viewGroup的行高

//如果宽度不够,则需换行

            if (childMesuredWidth + lineWidthUsed +mHorizontalSpacing > selfWidth){

allLines.add(lineViews);

                lineHeights.add(lineHeight);

                //一旦换行,可以判断当前所需的宽高了

                parentNeededWidth = Math.max(parentNeededWidth,lineWidthUsed+mHorizontalSpacing);

                parentNeededHeight = parentNeededHeight + lineHeight +mVerticalSpacing;

                lineViews =new ArrayList<>();

                lineWidthUsed =0;

                lineHeight =0;

            }

// view 是分行layout的,所以要记录每一行有哪些view,这样可以方便layout布局

            lineViews.add(childView);

            lineWidthUsed = lineWidthUsed + childMesuredWidth +mHorizontalSpacing;

            lineHeight = Math.max(lineHeight,childMesuredHeight);

            //childview 最后一行

            if (i == childCount -1){

lineHeights.add(lineHeight);

                allLines.add(lineViews);

                parentNeededWidth = Math.max(parentNeededWidth,lineWidthUsed );

                parentNeededHeight += lineHeight;

            }

//根据子View的度量结果,来重新度量自己ViewGroup

// 作为一个ViewGroup,它自己也是一个View,它的大小也需要根据它的父亲给它提供的宽高来度量

            int widthMode = MeasureSpec.getMode(widthMeasureSpec);

            int heightMode = MeasureSpec.getMode(heightMeasureSpec);

            int realWidth = (widthMode == MeasureSpec.EXACTLY)? selfWidth:parentNeededWidth;

            int realHeight = (heightMode == MeasureSpec.EXACTLY)?selfHeight:parentNeededHeight;

            setMeasuredDimension(realWidth,realHeight);

            isMeasuredOver =true;

        }

}

private void initMeasureParams() {

allLines =new ArrayList<>();

        lineHeights =new ArrayList<>();

    }

@Override

    protected void onLayout(boolean changed, int l, int t, int r, int b) {

int lineCount =allLines.size();

        int curL =0;

        int curT =0;

        for (int i =0;i

List lineViews =allLines.get(i);

            int lineHeight =lineHeights.get(i);

            for (int j =0;j

View view = lineViews.get(j);

                int left = curL;

                int top = curT;

                int bottom = top + view.getMeasuredHeight();

                int right = left + view.getMeasuredWidth();

                view.layout(left,top,right,bottom);

                curL = right +mHorizontalSpacing;

            }

curL =0;

            curT = curT + lineHeight +mVerticalSpacing;

        }

}

public static int dp2px(int dp) {

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().getDisplayMetrics());

    }

}

六 项目运行截图

296EC3A46E1EEFD584E9E00A73C164CF.jpg

github项目地址:https://github.com/doubizhu/flowlayout

有用请给我star qaq谢谢大家。

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

推荐阅读更多精彩内容