实战-自定义ViewGroup-流动布局(FlowLayout)

简介: 安卓自定义 ViewGroup 需要注意的地方至少需要提供 width, 和height两个属性同样地,如果要使用自定义的属性,那么就需要创建自己的名字空间,在Android Studio中,第三方的控件都使用如下代码来引入名字空间。xmlns:custom="http://schemas.android.com/apk/res-auto"

安卓自定义 ViewGroup 需要注意的地方



至少需要提供 width, 和height两个属性


同样地,如果要使用自定义的属性,那么就需要创建自己的名字空间,在Android Studio中,第三方的控件都使用如下代码来引入名字空间。


xmlns:custom="http://schemas.android.com/apk/res-auto"


流动布局手写精简版



  • 增加了'center'居中等三种排列方式


  • 额外支持padding属性


  • layout_newline属性支持自定义换行(类似'\n'的换行效果)


参考




下一步升级


  • 逆序排列子 view


  • 倒序排列子 view


  • 使每行 view 均分剩余空间


  • 可尝试增加目前流行的 tag 效果


FlowLayout.java

package cn.lik.view;
import java.util.ArrayList;
import java.util.List;
import cn.lik.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
/**
 * 流式布局
 * 
 * @author l1k
 */
public class FlowLayout extends ViewGroup {
    private static final String TAG = FlowLayout.class.getSimpleName();
    private static final boolean DEBUG = false;
    /**
     * 存储所有的View,按行记录
     */
    private ArrayList<List<View>> mAllViews = new ArrayList<List<View>>();
    /**
     * 记录每一行的最大高度
     */
    private ArrayList<Integer> mLineHeight = new ArrayList<Integer>();
    /**
     * The current value of the {@link AlignItems}, the default value is
     * {@link AlignItems#CENTER}.
     *
     * @see AlignItems
     */
    private int mAlignItems;
    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);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FlexLayout, defStyleAttr, 0);
        mAlignItems = a.getInt(R.styleable.FlexLayout_alignItems, AlignItems.CENTER);
        a.recycle();
    }
    @AlignItems
    public int getAlignItems() {
        return mAlignItems;
    }
    public void setAlignItems(@AlignItems int alignItems) {
        if (mAlignItems != alignItems) {
            mAlignItems = alignItems;
            requestLayout();
        }
    }
    @Override
    protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
        return new LayoutParams(p);
    }
    @Override
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParams(getContext(), attrs);
    }
    @Override
    protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }
    /**
     * 负责设置子控件的测量模式和大小 根据所有子控件设置自己的宽和高
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 获得它的父容器为它设置的测量模式和大小
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        if (DEBUG) {
            Log.i(TAG, "onMeasure: sizeWidth = " + sizeWidth + ", sizeHeight = " + sizeHeight);
        }
        // 如果是warp_content情况下,记录宽和高
        int width = 0;
        int height = 0;
        /**
         * 记录每一行的宽度,width不断取最大宽度
         */
        int lineWidth = 0;
        /**
         * 每一行的高度,累加至height
         */
        int lineHeight = 0;
        int cCount = getChildCount();
        final int paddingHorizontal = getPaddingLeft() + getPaddingRight();
        // 遍历每个子元素
        for (int i = 0; i < cCount; i++) {
            final View child = getChildAt(i);
            // 测量每一个child的宽和高
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            // 得到child的lp
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();
            // 当前子空间实际占据的宽度
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            // 当前子空间实际占据的高度
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            /**
             * 如果加入当前child,则超出最大宽度,则的到目前最大宽度给width,累加height 然后开启新行
             */
            if ((i!=0) && (lp.newline || (lineWidth + childWidth > sizeWidth - paddingHorizontal))) {
                width = Math.max(Math.max(lineWidth, childWidth), width);// 取最大
                lineWidth = childWidth; // 开启新行,记录宽度
                // 叠加当前高度,
                height += lineHeight;
                // 开启记录下一行的高度
                lineHeight = childHeight;// 开启新行, 记录高度
            } else { // 否则累加值lineWidth,lineHeight取最大高度
                lineWidth += childWidth;
                lineHeight = Math.max(lineHeight, childHeight);
            }
            // 如果是最后一个,则将当前记录的最大宽度和当前lineWidth做比较
            if (i == cCount - 1) {
                width = Math.max(width, lineWidth);
                height += lineHeight;
            }
        }
        super.setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth 
                : width + getPaddingLeft() + getPaddingRight(),
                (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight 
                        : height + getPaddingTop() + getPaddingBottom());
    }
    @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;
        int left = getPaddingLeft();
        int top = getPaddingTop();
        final int paddingHorizontal = left + getPaddingRight();
        // 存储每一行所有的childView
        List<View> lineViews = new ArrayList<View>();
        int cCount = getChildCount();
        // 遍历所有的孩子
        for (int i = 0; i < cCount; i++) {
            final View child = getChildAt(i);
            LayoutParams lp = (LayoutParams) child.getLayoutParams();
            int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
            // 当前子空间实际占据的高度
            int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
            // 如果已经需要换行
            if ((i!=0) && (lp.newline || (childWidth + lineWidth > width - paddingHorizontal))) {
                // 记录这一行所有的View以及最大高度
                mLineHeight.add(lineHeight);
                // 将当前行的childView保存,然后开启新的ArrayList保存下一行的childView
                mAllViews.add(lineViews);
                lineWidth = 0;// 重置宽
                lineHeight = 0;//重置高
                lineViews = new ArrayList<View>();
            }
            /**
             * 如果不需要换行,则累加
             */
            lineWidth += childWidth;
            lineHeight = Math.max(lineHeight, childHeight);
            lineViews.add(child);
        }
        // 记录最后一行
        mLineHeight.add(lineHeight);
        mAllViews.add(lineViews);
        // 得到总行数
        int lineNums = mAllViews.size();
        for (int i = 0; i < lineNums; i++) {
            // 每一行的所有的views
            lineViews = mAllViews.get(i);
            // 当前行的最大高度
            lineHeight = mLineHeight.get(i);
            if (DEBUG) {
                Log.e(TAG, "第" + i + "行 :" + lineViews.size() + " , " + lineViews);
                Log.e(TAG, "第" + i + "行, :" + lineHeight);
            }
            // 遍历当前行所有的View
            for (int j = 0; j < lineViews.size(); j++) {
                View child = lineViews.get(j);
                if (child.getVisibility() == View.GONE) {
                    continue;
                }
                MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
                // 计算childView的left,top,right,bottom
                int lc = 0;
                int tc = 0;
                int rc = 0;
                int bc = 0;
                switch (mAlignItems) {
                case AlignItems.FLEX_START:
                    lc = left + lp.leftMargin;
                    tc = top + lp.topMargin;
                    rc = lc + child.getMeasuredWidth();
                    bc = tc + child.getMeasuredHeight();
                    break;
                case AlignItems.FLEX_END:
                    lc = left + lp.leftMargin;
                    tc = top + lineHeight - child.getMeasuredHeight() - lp.bottomMargin;
                    rc = lc + child.getMeasuredWidth();
                    bc = tc + child.getMeasuredHeight();
                    break;
                case AlignItems.CENTER:
                    lc = left + lp.leftMargin;
                    tc = top + lp.topMargin
                            + (lineHeight - lp.topMargin - child.getMeasuredHeight() - lp.bottomMargin) / 2;
                    rc = lc + child.getMeasuredWidth();
                    bc = tc + child.getMeasuredHeight();
                    break;
                default:
                    throw new IllegalStateException("Invalid alignItems is set: " + mAlignItems);
                }
                child.layout(lc, tc, rc, bc);
                left += child.getMeasuredWidth() + lp.rightMargin + lp.leftMargin;
            }
            left = getPaddingLeft();
            top += lineHeight;
        }
    }
    public static class LayoutParams extends ViewGroup.MarginLayoutParams {
        /**
         * need newline or not
         */
        public boolean newline = false;
        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
            final TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.FlexLayout_Layout);
            newline = a.getBoolean(R.styleable.FlexLayout_Layout_layout_newline, false);
            a.recycle();
        }
        public LayoutParams(ViewGroup.LayoutParams lp) {
            super(lp);
        }
        public LayoutParams(ViewGroup.MarginLayoutParams lp) {
            super(lp);
        }
        /**
         * Copy constructor. Clones the width, height, margin values, newline
         *
         * @param lp The layout params to copy from.
         */
        public LayoutParams(LayoutParams lp) {
            super(lp);
            this.newline = lp.newline;
        }
        /**
         * Copy constructor. Clones the width, height
         *
         * @param lp The layout params to copy from.
         */
        public LayoutParams(int width, int height) {
            super(width, height);
        }
        /**
         * Copy constructor. Clones the width, height, newline
         *
         * @param lp The layout params to copy from.
         */
        public LayoutParams(int width, int height, boolean newline) {
            super(width, height);
            this.newline = newline;
        }
    }
}


AlignItems.java


/*
 * Copyright 2016 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.lik.view.flexbox;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/** This attribute controls the alignment along the cross axis. */
@Retention(RetentionPolicy.SOURCE)
public @interface AlignItems {
    /** Flex item's edge is placed on the cross start line. */
    int FLEX_START = 0;
    /** Flex item's edge is placed on the cross end line. */
    int FLEX_END = 1;
    /** Flex item's edge is centered along the cross axis. */
    int CENTER = 2;
}


res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="FlexLayout">
        <attr name="alignItems">
            <enum name="flex_start" value="0" />
            <enum name="flex_end" value="1" />
            <enum name="center" value="2" />
        </attr>
    </declare-styleable>
    <declare-styleable name="FlexLayout_Layout">
        <attr name="layout_newline" format="boolean" />
    </declare-styleable>
</resources>



目录
相关文章
|
Linux 网络安全 开发工具
python更换版本
python更换版本
964 1
|
NoSQL Java Redis
Spring boot整合Redis实现发布订阅(超详细)
Redis发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收信息。微信,微博,关注系统 Redis客户端可以订阅任意数量的频道
10143 0
Spring boot整合Redis实现发布订阅(超详细)
|
弹性计算 应用服务中间件
阿里云轻量应用服务器200M峰值带宽详细说明,200Mbps适用哪种使用场景?
阿里云轻量服务器提供200Mbps峰值带宽(上下行对等),理论下载速度约25MB/s,属共享型带宽,非持续保障,高峰时段可能受限。适合个人网站、测试开发等轻量应用,不适用于高并发或企业级业务。38元/年起,详见活动页。
5395 2
|
监控 Java 数据库
从零学 Dropwizard:手把手搭轻量 Java 微服务,告别 Spring 臃肿
Dropwizard 整合 Jetty、Jersey 等成熟组件,开箱即用,无需复杂配置。轻量高效,启动快,资源占用少,内置监控、健康检查与安全防护,搭配 Docker 部署便捷,是构建生产级 Java 微服务的极简利器。
1355 117
|
12月前
|
存储 数据采集 供应链
CNFans模式淘宝1688代购系统搭建指南
CNFans代购系统整合1688供应链与淘宝渠道,为跨境用户提供一站式代购服务。通过API对接实现商品、订单、支付数据自动化,支持多币种结算与全程物流追踪。面向小微电商、海外代购及价格敏感型消费者,提供代购、质检、仓储、物流等全流程服务,保障正品与性价比。盈利涵盖服务费、增值服务、广告与会员订阅,打造安全透明的跨境代购生态平台。(239字)
1097 0
|
运维 监控 网络协议
物联网设备状态监控全解析:从告警参数到静默管理的深度指南-优雅草卓伊凡
物联网设备状态监控全解析:从告警参数到静默管理的深度指南-优雅草卓伊凡
642 11
物联网设备状态监控全解析:从告警参数到静默管理的深度指南-优雅草卓伊凡
|
Go 微服务
Go语言微服务框架 - 3.日志库的选型与引入
衡量日志库有多个指标,我们今天重点关注两点:简单易用 与 高性能。简单易用是一个日志库能被广泛使用的必要条件,而高性能则是企业级的日志库非常重要的衡量点,也能在源码层面对我们有一定的启发。
1497 1
|
机器学习/深度学习 编解码 计算机视觉
YOLOv11改进策略【Head】| ASFF 自适应空间特征融合模块,改进检测头Detect_ASFF
YOLOv11改进策略【Head】| ASFF 自适应空间特征融合模块,改进检测头Detect_ASFF
2938 13
YOLOv11改进策略【Head】| ASFF 自适应空间特征融合模块,改进检测头Detect_ASFF
|
存储 人工智能 TensorFlow
你的旧电脑还跑得动AI吗?ToDesk云电脑/青椒云/顺网云算力支持实测报告
随着AI大模型的崛起及广泛应用,旧电脑显示出了硬伤,硬件配置方面,老旧的 CPU、GPU 性能不足,运算速度缓慢,导致 AI 模型训练和推理耗时极长。内存与存储有限,无法承载大型 AI 数据集和复杂模型。散热不佳,运行 AI 任务时易过热死机。且旧电脑可能不兼容新的 AI 框架与工具,软件更新困难。这使得用户在利用旧电脑探索 AI 时,体验大打折扣,而 ToDesk云电脑、青椒云、顺网云等或许能为解决这些问题带来新契机。今天就来实测一下常用的ToDesk云电脑/青椒云/顺网云,博主将从功能、优缺点等方面为你深度剖析这几款云电脑,帮你选出最适合的那一款!🏆
1920 5