Android自定义控件-流式布局(FlowLayout)

本文介绍了如何在Android中实现自定义Flow Layout,包括其工作原理、子项展示样式、adapter封装以及相关接口和布局应用。通过示例代码展示了如何创建一个能自动换行的流式布局。
摘要由CSDN通过智能技术生成

流布局,相信大家都知道,就是依次展示,每行展示的个数不定,此行不能展示出末尾的那个item,自动换行,我简单写了一个,下面是我的效果图,大家看一下,是否是自己要找的效果
在这里插入图片描述
下面贴出我的自定义flowLayout布局

/**
 * 打造一款具有滑动,并且不同高度的view会居中显示的流式布局
 */

public class FlowLayout extends ViewGroup implements FlowNotification {
   

    private static final String TAG = "yaya";
    //主要用来处理滑动用的,正直才可以滑动的
    protected int verticalOffset;

    protected int width, height;

    private LineDes row = new LineDes();

    protected int totalHeight;

    //用来存储每一行的高度,主要让子类去实现几行的流式布局
    protected SparseArray<Float> heightLines = new SparseArray<>();

    private FlowAdapter flowAdapter;

    private boolean isLineCenter;

    private static final boolean default_line_center = false;

    @Override
    public void onChange() {
   
        if (flowAdapter != null) {
   
            addItemView(flowAdapter);
        }
    }

    //用来存储信息的
    private class LineDes {
   
        //当前行的顶点坐标
        float cuLineTop;
        //行高度以最高的view为基准
        int lineHeight;
        //一行收集到的item
        List<ItemView> views = new ArrayList<>();

        //用完了该行的信息后进行清除的操作
        public void clearLineDes() {
   
            if (views.size() > 0) {
   
                views.clear();
            }
            lineHeight = 0;
            cuLineTop = 0;
        }
    }

    private class ItemView {
   
        View view;
        int heightArea;

        public ItemView(View view, int heightArea) {
   
            this.view = view;
            this.heightArea = heightArea;
        }
    }

    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 array = context.obtainStyledAttributes(attrs, R.styleable.FlowLayout);
        isLineCenter = array.getBoolean(R.styleable.FlowLayout_is_line_center, default_line_center);
        array.recycle();
        initArgus(context, attrs);
    }

    protected void initArgus(Context context, AttributeSet attrs) {
   
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int measureHeight = MeasureSpec.getSize(heightMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
   
            width = measureWidth;
        } else {
   
            //以实际屏宽为标准
            width = getContext().getResources().getDisplayMetrics().widthPixels;
        }

        Log.d(TAG, "屏高:" + getContext().getResources().getDisplayMetrics().heightPixels);

        int left = getPaddingLeft();
        int right = getPaddingRight()<
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值